【发布时间】:2019-04-25 09:09:06
【问题描述】:
场景:我有一个格式如下的文件。
@209.161.198.176/28 209.161.198.160/28 88 : 88 80 : 80 0x11/0xFF
@203.124.178.48/28 203.124.183.192/28 123 : 123 23 : 23 0x11/0xFF
@175.54.90.240/28 209.161.199.160/28 53 : 53 21 : 21 0x11/0xFF
@175.54.96.176/28 209.161.199.160/28 123 : 123 544 : 544 0x11/0xFF
@5.220.189.176/28 5.220.186.176/28 750 : 750 123 : 123 0x11/0xFF
.../*and the file contain about 100000 lines*/
每行可分为 5 个部分。
//For example:
(@209.161.198.176/28) (209.161.198.160/28) (88 : 88) (80 : 80) (00x11/0xFF)
我需要读取文件并将其存储到每个部分的 5 个多维数组中。第一个维度是它所在的行,第二个维度将存储它的字符串值。
//For example, the array to store the first section might have the following structure:
[line0][@209.161.198.176/28]
[line1][@203.124.178.48/28]
[line2][@175.54.90.240/28]
...
(array[line][string])
问题是我总是遇到分段错误,我不知道为什么。
这是我当前的代码:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
FILE *f;
char file1[100000][100], file2[100000][100], file3[100000][100], file4[100000][100], file5[100000][100];
char file = argv[1];
f = fopen(file,"r");
fscanf(f,"%s %s %s %s %s", file1[0], file2[0], file3[0], file4[0], file5[0]);
printf("%c%c",fike1[0][0],file1[0][1]);
fclose(f);
return 0;
}
在第 10 行,我尝试阅读第一行。在第 11 行,我尝试打印出第 0 行第一部分的前 2 个字符。
我能想到的可能问题是:
1) 我不能像这样直接打开 argv[1]。
2) 也许我需要在某处添加 * 或 & 但我找不到。
(OAO)
【问题讨论】:
-
代码有很多问题,最明显的是您没有声明
file或test,所以我不确定它会如何编译? -
1) [...] – 不,你不能。使用
fopen()打开文件并确保检查返回值是否有错误。您可能还想检查argc的值。完成文件后使用fclose()。此外,"%s %s %s %s %s"不适用于您想要的格式,因为"%s"会读取到下一个空格。像"88 : 88"这样的部分你想保持在一起但是包含空格。 -
代码中有很多错误,使用:
f = fopen(argv[1],"r"); fscanf(f,"%s %s %s %s %s", file1[0], file2[0], file3[0], file4[0], file5[0]);
标签: c arrays multidimensional-array scanf argv