October 21, 2024
Chicago 12, Melborne City, USA
C#

problems with C file acces arguments


I’m trying to learn C by making a basic daemon. I’m struggling to understand reading and writing files, since the arguments are correct to most posts and docs. My code is below:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<stdbool.h>
#include<stdint.h>
#include<string.h>
#include<signal.h>
#include <libnotify/notify.h>
#define EXIT_SUCCES 0
#define EXIT_FALIURE_READING_FILE 1
#define EXIT_FALIURE_LIBNOTIFY 2

char *ProgramTitle = "styckynotes";
char *messages_array[] = {"this mesage has not yet been set","this mesage has not yet been set","this mesage has not yet been set","this mesage has not yet been set","this mesage has not yet been set","this mesage has not yet been set","this mesage has not yet been set","this mesage has not yet been set","this mesage has not yet been set"};

int change_messgages(int array_number,char* message){
    if (array_number > 10){
        fprintf(stderr,"you are trying to write to a array number that doesnt exist, this would corrupt the memory please do not do that");
    }
    if (array_number < 10){
        char* intended_message= message;
        intended_message =  (char *)malloc(sizeof(char)*(strlen(intended_message)+1));
        messages_array[array_number-1] = intended_message;
        FILE myFile;
        myFile = fopen("messages.txt", "w");
        int i;
        if (myFile == NULL){
            printf("Error Reading File\n");
            exit (EXIT_FALIURE_READING_FILE);
        }
        for (i = 0; i < 16; i++){
            fprintf(myFile, "%d,", &messages_array[i] );
        }
        fclose(myFile);
    }
    return 0;
}

int signal_handler(void){
    //im counting from one since thats how an avarge user would experience it
    rl_bind_keyseq("\\C-e-1",note_pls(0));
    rl_bind_keyseq("\\C-e-2",note_pls(1));
    rl_bind_keyseq("\\C-e-3",note_pls(2));
    rl_bind_keyseq("\\C-e-4",note_pls(3));
    rl_bind_keyseq("\\C-e-5",note_pls(4));
    rl_bind_keyseq("\\C-e-6",note_pls(5));
    rl_bind_keyseq("\\C-e-7",note_pls(6));
    rl_bind_keyseq("\\C-e-8",note_pls(7));
    rl_bind_keyseq("\\C-e-9",note_pls(8));
    // rl_bind_keyseq("\\C-e-0",exit(EXIT_SUCCES));
    return 0;
}

note_pls(int pull_from){
    char* notification = notify_notification_new("reminder", messages_array[pull_from], "dialog-information");
    if (notification == NULL) {
        fprintf(stderr, "Got a null notify handle!\n");
    }
    notify_notification_show(notification, NULL);
    return 0;
}

int main(void){
    signal_handler();
    bool notifyInitStatus = notify_init(ProgramTitle);
    if (!notifyInitStatus) {
        fprintf(stderr, "Error initialising with libnotify!\n");
        exit(EXIT_FALIURE_LIBNOTIFY);
        FILE* myFile;
        myFile = fopen("messages.txt", "r");
        int i;
        if (myFile == NULL){
            printf("Error Reading File\n");
            exit (EXIT_FALIURE_READING_FILE);
        }

        for (i = 0; i < 16; i++){
            fgets(myFile,696969, &messages_array[i] );
        }
        fclose(myFile);
    }
}
stickynotes.c: In function ‘change_messgages’:
stickynotes.c:25:14: error: incompatible types when assigning to type ‘FILE’ from type ‘FILE *’
   25 |     myFile = fopen("messages.txt", "w");
      |              ^~~~~
stickynotes.c:27:16: error: invalid operands to binary == (have ‘FILE’ and ‘void *’)
   27 |     if (myFile == NULL){
      |                ^~
stickynotes.c:32:17: error: incompatible type for argument 1 of ‘fprintf’
   32 |         fprintf(myFile, "%d,", &messages_array[i] );
      |                 ^~~~~~
      |                 |
      |                 FILE
In file included from stickynotes.c:1:
/usr/include/stdio.h:357:38: note: expected ‘FILE * restrict’ but argument is of type ‘FILE’
  357 | extern int fprintf (FILE *__restrict __stream,
      |                     ~~~~~~~~~~~~~~~~~^~~~~~~~
stickynotes.c:34:16: error: incompatible type for argument 1 of ‘fclose’
   34 |         fclose(myFile);
      |                ^~~~~~
      |                |
      |                FILE
/usr/include/stdio.h:184:26: note: expected ‘FILE *’ but argument is of type ‘FILE’
  184 | extern int fclose (FILE *__stream) __nonnull ((1));
      |                    ~~~~~~^~~~~~~~
stickynotes.c:76:15: warning: passing argument 1 of ‘fgets’ from incompatible pointer type [-Wincompatible-pointer-types]
   76 |         fgets(myFile,696969, &messages_array[i] );
      |               ^~~~~~
      |               |
      |               FILE *
/usr/include/stdio.h:654:38: note: expected ‘char * restrict’ but argument is of type ‘FILE *’
  654 | extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
      |                     ~~~~~~~~~~~~~~~~~^~~
stickynotes.c:76:30: warning: passing argument 3 of ‘fgets’ from incompatible pointer type [-Wincompatible-pointer-types]
   76 |         fgets(myFile,696969, &messages_array[i] );
      |                              ^~~~~~~~~~~~~~~~~~
      |                              |
      |                              char **
/usr/include/stdio.h:654:69: note: expected ‘FILE * restrict’ but argument is of type ‘char **’
  654 | extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
      |                                                    ~~~~~~~~~~~~~~~~~^~~~~~~~

Reading up on the docs, checking forum posts, changing types.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video