【问题标题】:Pointer error in my program [C89]我的程序中的指针错误 [C89]
【发布时间】:2014-02-24 19:20:33
【问题描述】:

这个程序的目的是遍历用户指定的数据,所有数据都被格式化为 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);
}

【问题讨论】:

    标签: file pointers gcc segmentation-fault c89


    【解决方案1】:

    这一行是问题:

    for(i=*argv[argc-2];i>=*argv[(argc-1)];i++)
    

    argv[?] 不是 int。您必须使用 strtol() 系列函数之一将字符串转换为 int。

    你的代码还有很多其他问题:

    1) 检查用户是否提供了足够的参数(因为您使用的是argv[])。

    2) 检查fopen() 的返回值。

    3) while(!feof(fp)) 可能不是您想要的。 feof() 告诉您是否已阅读文件末尾。阅读:Why is “while ( !feof (file) )” always wrong?

    4) 缓冲区intbuff[] 只能容纳四个整数,而您似乎存储的整数与用户参数提供的一样多。

    【讨论】:

      【解决方案2】:

      argv[argc-2] 是一个 c 字符串(char* 类型)。取消引用后,您将获得第一个字符。然后,您将 ASCII 值转换为 int - 这不会为您提供该 char 的实际值(更不用说该参数的整个数值) - 而是在参数上使用 atoi 以获得真正的整数。

      【讨论】:

        【解决方案3】:

        使用while (!feof(fp)) 是一种不好的做法。
        您假设fgets() 正在成功。

        试试这样的:

        while (fgets(&buff[0],50,fp) != NULL)
        

        这一行也没有多大意义:

        for(i=*argv[argc-2];i>=*argv[(argc-1)];i++)
        

        我会尝试像这样设置您的代码:

        #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 ;
        };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-01-21
          • 2022-12-05
          • 1970-01-01
          • 1970-01-01
          • 2014-04-12
          • 2016-05-22
          • 1970-01-01
          相关资源
          最近更新 更多