我需要读取一个文件,并在每个时间步长将每一行(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 %dn", numbers[j], matrix[j]);
return(0);
}
感谢大家帮助我。
一种可能的方法 使用以下步骤:
1(
获取文件指针:FILE *fp = fopen("c:\dir1\file.txt", "r");
2(以两种方式使用fgets()
:
首先计算文件中的行数,并获得最长的行(提示:为int数计算空格(
(使用此信息,您可以为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
以下代码将提供文件中的行数和最高ints/行数,
然后创建您的阵列:
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);
}
#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 columnsn", 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);
}