我需要读取一个文件并在每个时间步骤存储整数数组中的每一行(int),然后在这个数组中工作。
输入如下:
0 16 12
1 10 17
2 14 8
3 12 17 16
9 14 16 19 13 5
19 16 6 17 11 15 9 4 12 18 8
然后使用这样的东西,我可以读取并打印每一行,但每次都不能将每行保存在一个数组中。
char matrix[500][500], space;
int numbers[500], i = 0, j;
int elementsA[10000];
FILE *fp = fopen("mygraph", "r");
int blabla[1000000];
int a;
while(!feof(fp))
{
fscanf(fp, "%d", &numbers[i]); // getting the number at the beggining
fscanf(fp, "%c", &space); // getting the empty space after the number
fgets(matrix[i++], 500, fp); //getting the string after a number
a ++;
}
for(j = 0; j < i; j++)
printf("%d %s %d\n", numbers[j], matrix[j]);
return(0);
}
感谢大家帮助我。
答案 0 :(得分:0)
一种可能的方法 使用以下步骤:
1) 获取文件指针:FILE *fp = fopen("c:\\dir1\\file.txt", "r");
2) 以两种方式使用fgets()
:
首先 计算文件中的行数,并获取最长行(提示:计算总数的空格)
(使用此信息,您可以为2D int
数组创建和分配内存)
第二次 从头开始重新开始一次读取一行
3) 使用strtok()
(或strtok_r()
)将每一行解析为整数并放入阵列中
4) 释放所有内存并退出。
以下是获取分配数组信息的方法:
提供输入 (每行必须包含\ n,就像这一行一样):
0 16 12
1 10 17
2 14 8
3 12 17 16
9 14 16 19 13 5
19 16 6 17 11 15 9 4 12 18 8
以下代码将提供行数和最大数量的整数/行文件,
然后创建你的数组:
int GetParamsOfFile(char *path, int *numInts) ;
int main(void)
{
int lines;
int mostInts;
int **array=0;
lines = GetParamsOfFile("place your path here", &mostInts);
// rows columns (will correlate with rows and column of your file)
array = Create2D(array, lines, mostInts);//creates array with correct number of rows and columms;
//Here is where you would re-read your file, this time, populating your new array.
//I will leave this part to you...
free2DInt(array, mostInts);
return 0;
}
int GetParamsOfFile(char *path, int *numInts)
{
FILE *fp = fopen(path, "r");
int spaces=0, lines=0, sK=0;
char *line; //choose big here
char *buf;
line = malloc(1000);
line = fgets(line, 1000, fp);
while(line)
{
lines++;
spaces = 0;
buf = strtok(line, " ");
while(buf)
{
spaces++;
buf = strtok(NULL, " ");
}
(spaces > sK)?(sK = spaces):(sK==sK);
line = fgets(line, 1000, fp);
}
*numInts = sK;
fclose(fp);
free(line);
return lines;
}
int ** Create2D(int **arr, int rows, int cols)
{
int space = cols*rows;
int y;
arr = calloc(space, sizeof(int));
for(y=0;y<rows;y++)
{
arr[y] = calloc(cols, sizeof(int));
}
return arr;
}
void free2DInt(int **arr, int rows)
{
int i;
for(i=0;i<rows; i++)
if(arr[i]) free(arr[i]);
free(arr);
}
答案 1 :(得分:0)
#include <stdio.h>
#include <ctype.h>
int countitems(char * s) {
int i;
for (i=0; *s; i++) {
while (*s && isblank(*s++));
while (*s && !isblank(*s++));
}
return(i);
}
char * nextitem(char *s) {
while (*s && !isblank(*s)) s++;
while (*s && isblank(*s)) s++;
return (s);
}
char * skipblanks(char *s) {
while (*s && isblank(*s)) s++;
return (s);
}
int main() {
FILE *f;
int i, j;
char s[100];
char * ss;
f = fopen("mygraph.txt", "r");
i = 0;
while (!feof(f)) {
fgets(s, 99, f);
j = countitems(s);
if (j>i) i = j;
}
printf("you need %d columns\n", i);
// allocate matrix
rewind(f); // at leat once in a life...rewind!!
while(!feof(f)) {
fgets(s, 99, f);
ss = skipblanks(s);
while (*ss) {
sscanf(ss, "%d", &j);
printf("%d ", j); // do something with numbers
ss = nextitem(ss);
}
printf("\n");
}
fclose(f);
}