Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将文本文件中的行读入数组_C_Arrays_File - Fatal编程技术网

C 将文本文件中的行读入数组

C 将文本文件中的行读入数组,c,arrays,file,C,Arrays,File,我有一个文本文件,其中包含以下行: (0,0) -180.000 77.500 -999.000 -999.000 -999.000 2740.831 45.000 -0.001 -0.001 0.000 458.138 45.000 -999.000 (1,0) -179.500 77.500 -999.000 -999.000 -999.000 2740.831 45.000 -0.001 -0.001 0.000 458.138 45.000 -99

我有一个文本文件,其中包含以下行:

(0,0) -180.000  77.500  -999.000  -999.000  -999.000  2740.831  45.000  -0.001  -0.001  0.000 458.138 45.000  -999.000
(1,0) -179.500  77.500  -999.000  -999.000  -999.000  2740.831  45.000  -0.001  -0.001  0.000 458.138 45.000  -999.000
(2,0) -179.000  77.500  -999.000  -999.000  -999.000  2740.831  45.000  -0.001  -0.001  0.000 458.138 45.000  -999.000
(3,0) -178.500  77.500  -999.000  -999.000  -999.000  2740.831  45.000  -0.001  -0.001  0.000 458.138 45.000  -999.000
...
...
(359,0) -0.500  77.500  -999.000  -999.000  -999.000  2740.831  45.000  -0.001  -0.001  0.000 458.138 45.000  -999.000
我试图使用以下程序将此文本文件(buf)的每一行放入数组(buffarray)的单个元素中:

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

#define PI 4*atan2(1,1)

int main(int argc, char *argv[]) {
    FILE *fp;
    char buf[200];
    char *token;
    char buffarray[223920];
    char filename[150];
    int i, j, k;

    sscanf(argv[1], "%s", filename);

    if ((fp = fopen(filename, "rt")) == NULL) {
        printf("Failed in fopen: %s\n", filename);
        return -1;
    }

    while (!feof(fp)) {
        fgets(buf, 200, fp);
        token = buf;
        printf("buf is %s\n", buf);     
        buffarray++ = token;
    }
}
我如何解决这个问题?理想情况下,我希望创建另一个文本文件,在该文件中重新排列行,以便首先在新文本文件中打印原始文本的第180至359行,然后在新文本文件中打印第1至179行。

多个问题:

  • PI
    宏未正确插入括号。它应该是
    #定义PI(4*atan2(1,1))
  • 而(!feof(fp))
    总是错误的。使用
    while(fgets(buf,200,fp))
    代替
  • 不能增加数组,您希望将数组末尾的字符串与strcat(buffarray,token)连接起来但必须在循环之前将
    buffarray[0]
    初始化为
    '\0'
以下是更正的版本:

#包括
#包括
#包括
#包括
#包括
#定义PI(4*atan2(1,1))
int main(int argc,char*argv[]){
文件*fp;
char-buf[200];
字符*令牌;
字符数组[223920];
字符文件名[150];
如果(argc<2 | | sscanf(argv[1],%149s,文件名)!=1){
printf(“缺少命令行参数\n”);
返回1;
}
if((fp=fopen(文件名,“rt”))==NULL){
printf(“在fopen%s中失败:%s\n”,文件名,strerror(errno));
返回1;
}
*buffarray='\0';
而(fgets(buf、sizeof buf、fp)){
令牌=buf;
printf(“buf是%s\n”,buf);
strcat(buffarray,token);
}
fclose(fp);
printf(“文件内容:\n);
fput(buffarray,stdout);
返回0;
}

检查许多问题中的一个:我应该使用什么?仔细看看
buffarray++=token;
。这真的是你想要的吗?如果你只想将所有字符读入一个大数组,那么你应该使用
fread()
而不是
fgets()
。注意:除了
#定义PI(4*atan2(1,1))
-->
acos(-1)
@chux:是的。我想知道为什么标准不要求在
中定义与目标浮点表示的最佳近似值。
PI
在发布的代码中没有使用:)当我在上面的程序中创建数组时。我如何设置它以便打印buffarray[I]例如,如果我想打印出数组中的一个元素,我该怎么做?@jms1980:如果你的目标是解析一个文本文件,将文本转换为数字,那么这个方法是不合适的。你可能应该使用
sscanf()
将行转换为一组值,存储在一组结构中。您能澄清这个问题吗?
translate_ww3file.c: In function ‘int main(int, char**)’:
translate_ww3file.c:30:12: error: lvalue required as increment operand
   buffarray++ = token;
        ^