File 我的程序中的指针错误[C89]

File 我的程序中的指针错误[C89],file,pointers,gcc,segmentation-fault,c89,File,Pointers,Gcc,Segmentation Fault,C89,该程序的目的是通过用户指定的数据运行,所有数据的格式为hw-data-3.txt,其中3可以从1到100不等。我需要遍历指定的文件,并将总共花费的$。每行最多有50个字符,包括\n以及每个文件最多30行。我得到了一个分段错误,我很确定这是一个指针问题,但我不确定它在哪里。谁能帮我找到它 #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { char buff[255];i

该程序的目的是通过用户指定的数据运行,所有数据的格式为hw-data-3.txt,其中3可以从1到100不等。我需要遍历指定的文件,并将总共花费的$。每行最多有50个字符,包括\n以及每个文件最多30行。我得到了一个分段错误,我很确定这是一个指针问题,但我不确定它在哪里。谁能帮我找到它

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
    char buff[255];int spent[30]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,},i,z,intbuff[]={0,0,0,0};
    for(i=*argv[argc-2];i>=*argv[(argc-1)];i++)
    {
            sprintf(buff,"hw-data-%d.txt",i);
            FILE *fp;fp=fopen(buff,"r");    /*Initializing and setting file pointer for unknown amount of files*/
            buff[0]='\0';
            while(!feof(fp))    /*while the end of file has not been reached for the stream continue doing these actions*/
            {
                fgets(&buff[0],50,fp); /*captures 1 line at a time from stream then advances the streams position*/
                for(z=0;buff[z]!='\0';z++){
                    if(buff[z]=='$')
                        break;  /*breaks loop at position in line where $ occurs*/}
                for (i=0;i<2;i++,z++){
                    if (buff[z]=='0'||buff[z]=='1'||buff[z]=='2'||buff[z]=='3'||buff[z]=='4'||buff[z]=='5'||buff[z]=='6'||buff[z]=='7'||buff[z]=='8'||buff[z]=='9')
                        intbuff[i]=buff[z];
                    else
                        break;}/* break statement is here to preserve number of integers after $, ie. $100 i=3 $33 i=2 $9 i=1 */
                for (;i>=0;--i)
                {
                    intbuff[3]+=buff[i];
                }               
                for(i=0;i<30;i++)
                {(spent[i]==0)?(spent[i]=intbuff[3]):(0);}/* If i in int array is 0 then replace 0 with the number captured.*/
            }
            fclose(fp);
    }
    return(0);
}

这一行就是问题所在:

for(i=*argv[argc-2];i>=*argv[(argc-1)];i++)
argv[?]不是int。必须使用一个族函数将字符串转换为int

您的代码还有许多其他问题:

1检查用户在使用argv[]时是否提供了足够的参数

2检查fopen的返回值

3分钟!feoffp不太可能是你想要的。feof告诉您是否已读过文件末尾。阅读:

4缓冲区intbuff[]只能保存四个int,而您似乎存储了用户参数提供的尽可能多的int。

while!feoffp是一种糟糕的做法。 您假设fgets正在成功

试着这样做:

while (fgets(&buff[0],50,fp) != NULL)
#include <stdio.h>
#include <stdlib.h>

#define LINE_LENGTH 50
#define MAX_LINES 30

int main (int argc, char *argv [])
{
    int i ;

    char buffer [LINE_LENGTH] = {0} ;

    // Assuming argv[0] is the program name.
    for (i = 1; i < argc; ++i) {
        FILE *fp ;
        fp = fopen (argv [i], "r") ;

        if (fp == NULL) {
            // Handle error...
        }

        while (fgets (buffer, LINE_LENGTH, fp) != NULL) {
            // ...
        }
    }

    return 0 ;
};
这句话也没什么意义:

for(i=*argv[argc-2];i>=*argv[(argc-1)];i++)
我会尝试如下设置您的代码:

while (fgets(&buff[0],50,fp) != NULL)
#include <stdio.h>
#include <stdlib.h>

#define LINE_LENGTH 50
#define MAX_LINES 30

int main (int argc, char *argv [])
{
    int i ;

    char buffer [LINE_LENGTH] = {0} ;

    // Assuming argv[0] is the program name.
    for (i = 1; i < argc; ++i) {
        FILE *fp ;
        fp = fopen (argv [i], "r") ;

        if (fp == NULL) {
            // Handle error...
        }

        while (fgets (buffer, LINE_LENGTH, fp) != NULL) {
            // ...
        }
    }

    return 0 ;
};
argv[argc-2]是一种c字符串类型的char*。取消引用后,将获得第一个字符。然后将ASCII值转换为整数-这不会给出该字符的实际值,更不用说该参数的整个数值-而是对参数使用atoi来获得实际整数