【问题标题】:I need an explanation with File I/O I am recieving warnings I do not understand我需要 File IN 的解释 我收到了我不明白的警告
【发布时间】:2017-10-23 20:54:27
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>


struct string_count_struct{
    char fname;
    char str;
    long long count;

};



void* filesearch(void* arg)
{

    //get the file name
    struct string_count_struct *arg_ptr = (struct string_count_struct*) arg;

    int line_num = 1;
    int find_result = 0;
    char temp[512];


    //create a file pointer 
    FILE *fp;
    fp = fopen(arg_ptr -> fname, "r");

    //dont forget error handling
    if (fp == NULL){
    printf("File could not be opened");
    return(-1);
    }

    while (fgets(temp, 512, fp) != NULL) {
        if ((strstr(temp, arg_ptr -> str)) != NULL) {
            find_result++;
        }
        line_num++;
    }

    if(find_result = 0) {
        printf("\nSorry, couldn't find a match.\n");
    }

    arg_ptr -> count = find_result;

    //close the file
    if (fp){
        fclose(fp);
    }   

    pthread_exit(0);


}


int main (int argc, char **argv)
{


    if (argc < 3) {
    printf("Usage: <file> <string> <arg1> <arg2>...<argN>\n", argv[0]);
    exit(-1);
    }

    int num_args = argc - 2;

    struct string_count_struct args[num_args];

    //Thread Creation:
    pthread_t tids[num_args];

    for(int i = 0; i < num_args; i++) {
        args[i].fname = atoll(argv[i + 2]);

        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_create(&tids[i], &attr, filesearch, &args[i]);
    }

    //Wait until work is completed
    for (int i = 0; i < num_args; i ++){
    pthread_join(tids[i], NULL);
    printf("blah is blah %lld\n", args[i].count);
    }





return 0;
}

这是我的警告

root@kali:~/Desktop# gcc prog2.c -lbthread
prog2.c: In function ‘filesearch’:
prog2.c:29:13: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [-Wint-conversion]
  fp = fopen(arg_ptr -> fname, "r");
             ^~~~~~~
In file included from prog2.c:1:0:
/usr/include/stdio.h:274:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
 extern FILE *fopen (const char *__restrict __filename,
              ^~~~~
prog2.c:34:8: warning: return makes pointer from integer without a cast [-Wint-conversion]
  return(-1);
        ^
prog2.c:38:21: warning: passing argument 2 of ‘strstr’ makes pointer from integer without a cast [-Wint-conversion]
   if ((strstr(temp, arg_ptr -> str)) != NULL) {
                     ^~~~~~~
In file included from prog2.c:4:0:
/usr/include/string.h:337:14: note: expected ‘const char *’ but argument is of type ‘char’
 extern char *strstr (const char *__haystack, const char *__needle)
              ^~~~~~
prog2.c: In function ‘main’:
prog2.c:78:17: error: assignment of read-only member ‘fname’
   args[i].fname = atoll(argv[i + 2]);

我不确定自己做错了什么,这些错误使我的程序无法正确读取所需文件并计算用户将选择的特定字符串的出现次数。 我已经修复了我的错误,但没有修复警告

程序将采用命令行参数,为要搜索的每个文件创建一个单独的线程,搜索每个文件,然后给出结果。我计划使用 Mutex 进行进一步改进,但现在我只是想解决我的 I/O 问题。

【问题讨论】:

    标签: linux file pointers pthreads posix


    【解决方案1】:

    只是解决一些警告,我完全不确定这是否会使代码工作:

    在第 29 行,fopen 需要一个文件名 (char *),但 fname 只是 string_count_struct 中的一个 char。将其设为char*。在主函数中,您将参数之一从 ASCII 转换为 long long 并将其分配给 fname,稍后将用作 fopen() 的文件名。这可能不是你想要做的。

    在第 34 行,您返回 -1,它不是指针。您声明该函数将返回一个 void 指针。使其返回(0)(或返回(NULL))。

    与第 38 行中的第 29 行相同:结构 string_count_struct 中的 str 只是一个 char,但 strstr 需要一个 char*。也将其设为char*

    您的“用法”缺少用于实际打印参数 argv[0] 的“%s”格式字符串。

    【讨论】:

    • 感谢您的回复,非常感谢。你的建议帮助我完善了我的工作
    猜你喜欢
    • 2023-03-07
    • 2019-07-26
    • 2013-01-02
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2011-07-04
    相关资源
    最近更新 更多