我有一个程序,输入一个字符串';s称为搜索,这是目标,我想在csv文件中搜索;搜索“;有 #包括 #包括 void myFgets(char str[],int n); int main(int argc,字符**argv) { 如果(argc搜索从文件内容搜索的值。< /P>你的第一步是弄清楚你是在写C还是C++代码。它们是两种完全不同的语言。你打算写一个C或C++程序吗? while ((dh = (char)fgetc(fileRead)) != ',') { str[i] = dh; i++; } str[i] = '\0'; /* add this to terminate the string */

我有一个程序,输入一个字符串';s称为搜索,这是目标,我想在csv文件中搜索;搜索“;有 #包括 #包括 void myFgets(char str[],int n); int main(int argc,字符**argv) { 如果(argc搜索从文件内容搜索的值。< /P>你的第一步是弄清楚你是在写C还是C++代码。它们是两种完全不同的语言。你打算写一个C或C++程序吗? while ((dh = (char)fgetc(fileRead)) != ',') { str[i] = dh; i++; } str[i] = '\0'; /* add this to terminate the string */,c++,c,csv,file,fopen,C++,C,Csv,File,Fopen,在第39行我得到一个错误,但idk为什么看起来我做的一切都很好 我试着在行中循环并按“,”分隔,这样我就可以检查search==是否指向它,但它不是wokring im使用函数strstr相互比较两个字符串它工作得很好,但唯一的问题是在dh 我在dh之后进行了fseek,这样我就不会在ch循环中的错误位置写入内容了您忘记了终止字符串 #include <stdio.h> #include <string.h> void myFgets(char str[], int n

在第39行我得到一个错误,但idk为什么看起来我做的一切都很好 我试着在行中循环并按“,”分隔,这样我就可以检查search==是否指向它,但它不是wokring im使用函数strstr相互比较两个字符串它工作得很好,但唯一的问题是在dh
我在dh之后进行了fseek,这样我就不会在ch循环中的错误位置写入内容了

您忘记了终止字符串

#include <stdio.h>
#include <string.h>

void myFgets(char str[], int n);

int main(int argc, char** argv) 
{
    if (argc < 2)
    {
        printf("Usage: csv <csv file path>\n");
        return 1;
    }
    else
    {
        char ch = ' ', search[100], dh = ' ';
        int row = 1;
        printf("Enter value to search: ");
        myFgets(search, 100);

        FILE* fileRead = fopen(argv[1], "r");

        if (fileRead == NULL)
        {
            printf("Error opening the file!\n");
            return 1;
        }

        while ((ch = (char)fgetc(fileRead)) != EOF)
        {
            char str[100];
            int i = 0, pos = ftell(fileRead);
            while ((dh = (char)fgetc(fileRead)) != ',')
            {
                str[i] = dh;
                i++;
            }
            fseek(fileRead, pos + 1, SEEK_SET);
            if (strstr("\n", str) != NULL)
            {
                row++;
            }
            if (strstr(search, str) != NULL)
            {
                printf("Value was found in row: %d\n", row);
                break;
            }
        }
    }

    getchar();
    return 0;
}

/*
Function will perform the fgets command and also remove the newline
that might be at the end of the string - a known issue with fgets.
input: the buffer to read into, the number of chars to read
*/
void myFgets(char* str, int n)
{
    fgets(str, n, stdin);
    str[strcspn(str, "\n")] = 0;
}

<>(Struts(Search,STR)!= null)应该是<代码>(如果STRSTR(STR,Search)!= null)< /C>搜索从文件内容搜索的值。< /P>你的第一步是弄清楚你是在写C还是C++代码。它们是两种完全不同的语言。你打算写一个C或C++程序吗?
while ((dh = (char)fgetc(fileRead)) != ',')
{
    str[i] = dh;
    i++;
}
str[i] = '\0'; /* add this to terminate the string */