C 文件访问参数的问题

问题描述 投票:0回答:1

我正在尝试通过制作一个基本的守护进程来学习 C。我正在努力理解读取和写入文件,因为这些参数对于大多数帖子和文档都是正确的。我的代码如下:

#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)
      |                                                    ~~~~~~~~~~~~~~~~~^~~~~~~~

阅读文档、检查论坛帖子、更改类型。

c
1个回答
2
投票

编译器错误的原因是因为在

change_messgages()
内部您已将
myFile
变量声明为
FILE
而不是
FILE*
,就像您对
myFile
内部的
main()
变量所做的那样。


您的代码还存在其他问题。

change_messgages()

  • 您正在分配一个与输入

    char[]
    长度相同的新
    message
    字符串,但您并未将
    message
    的内容复制到新字符串中。 您可以使用
    strdup()
    来达到此目的。 但无论哪种方式,如果指定的
    array_number
    已经由之前对
    change_messgages()
    的调用设置,那么您就会泄漏之前
    malloc()
    ' 的内存,因为您没有
    free()
    ' 操作它。 但是,
    messages_array
    是用
    char*
    指向字符串文字的指针初始化的,因此您无法知道何时可以安全地
    free()
    中的现有字符串。
    
    

  • messages_array

    中只有 9 个元素,但您的

    messages_array
    循环正在迭代 16 个元素。 您超出了数组的范围。
    
    

  • 您正在将
  • fprintf()

    指针传递给

    char*
    中的
    %d
    说明符。该说明符需要
    fprintf()
    而不是
    int
    。 如果您尝试打印
    char*
    指向的字符串的内容,请改用
    char*
    说明符。
    
    

%s


  • main()

    中只有 9 个元素,但您的

    messages_array
    循环正在迭代 16 个元素。 您超出了数组的范围。
    
    

  • fgets()

    使用指向字符串文字的

    messages_array
    指针进行初始化,并且在读取
    char*
    文件之前不会替换这些指针,因此您尝试将文件中的数据读取到只读内存中。更糟糕的是,您告诉
    messages.txt
    它可以从文件中读取
    fgets()
    个字符,但您并没有尝试分配任何接近那么大的内存。
    
    

© www.soinside.com 2019 - 2024. All rights reserved.