gpt4 book ai didi

c - 将文件 .txt 读取到数组并拆分

转载 作者:行者123 更新时间:2023-11-30 18:58:28 24 4
gpt4 key购买 nike

我来这里是为了寻求帮助。我想读取以下格式的 .txt:

20
20

1 2
1 2
3 4 2 2
5 4 1 1 1 1
4 2 1 3 1
3 2 1 7
4 1 3 3 2
2 10 1
5 8 2 1 1 1
3 8 1 1
4 9 1 1 1
4 3 8 1 1
2 2 12
4 3 2 4 3
4 2 2 4 3
3 3 4 2
2 3 2
3 2 1 2
2 5 4
2 4 2

1 6
1 9
2 9 3
4 4 4 1 1
3 1 2 8
3 4 9 1
4 2 7 1 1
2 7 1
3 1 5 1
3 8 5 1
4 1 3 7 1
5 1 2 2 3 1
5 2 1 2 2 1
3 2 4 1
4 2 1 4 1
6 1 1 1 1 2 1
4 1 2 1 2
3 8 1 2
1 4
1 2

我写了一些代码。它复制 .txt 的每一行(我不想读取 20 行)并在运行时直接在 cmd 中打印。这是代码:

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



int main( int argc, char *argv[]){

FILE*fp;
int i=0, dimension[2];
char row[128];
int n_rows, n_columns;
char *v_row, *v_col;


fp=fopen(argv[1],"r");


if(fp == NULL){
printf("File not found\n");
exit(0);
}
while(i<3){

fgets(row, 128, fp);
sscanf(row, "%d", &dimension[i]);
i++;
}
n_rows=dimension[0];
n_columns=dimension[1];

printf("dimension: %d x %d\n\n", n_rows, n_columns);
v_row=malloc((n_rows+1)*sizeof(char));
v_col=malloc((n_columns+1)*sizeof(char));

for(i=0; i<n_rows; i++){
fgets(row, 128, fp);
printf("row: %s\n", row);


}
for(i=0;i<n_columns; i++){
fgets(row, 128, fp);
printf("row: %s\n", row);
sscanf(row,"%s", &v_col[i]);
}


fclose(fp);

exit(-1);
}

但这不是我的最终目标。我真正想做的是将每一行放入一个数组中,将第一个数字拆分为一个数组,并将该行的其余部分拆分为另一个数组,但不幸的是我无法找到一种方法来做到这一点。

最佳答案

其实你自己已经得到了答案。使用 sscanf() 完成您的工作

示例::

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

int main()
{
char *tmp = "123 456 789 123 4652 128793";
char arr[5], arr2[30];
printf("tmp::\t%s\n",tmp);
sscanf(tmp,"%s %[^\n]s",arr,arr2);
printf("arr::\t%s\n",arr);
printf("arr2::\t%s\n",arr2);
return 0;
}

输出:

tmp::   123 456 789 123 4652 128793
arr:: 123
arr2:: 456 789 123 4652 128793

编辑::

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 42 // max length of line

int main()
{
FILE *fp; // for the file
int Nrow, Ncol;
int loop;
char line[LENGTH];
char **arrRows_1stDim, **arrRows_2ndDim;
char **arrCols_1stDim, **arrCols_2ndDim;
char arr[20], arr2[20];
fp = fopen("testDATA.txt","r"); // I saved your file with this name
if(fp == NULL)
{
perror("testDATA.txt");
return -1;
}
fscanf(fp,"%d", &Nrow);
fscanf(fp,"%d", &Ncol);
printf("Nrow::%d,\tNcol::%d\n",Nrow,Ncol);
arrRows_1stDim = malloc(sizeof(char*) * Nrow);
arrRows_2ndDim = malloc(sizeof(char*) * Nrow);
arrCols_1stDim = malloc(sizeof(char*) * Ncol);
arrCols_2ndDim = malloc(sizeof(char*) * Ncol);
// check if our malloc() was allocated
if(arrRows_1stDim == NULL || arrRows_2ndDim == NULL || arrCols_1stDim == NULL || arrCols_2ndDim == NULL)
{
fprintf(stderr,"Couldn't malloc()");
return -1;
}
printf("Reading Rows...\n\n");
for(loop=0;loop<Nrow;loop++)
{
fgets(line,LENGTH,fp);
if(strlen(line)==1)
loop--;
else
{
sscanf(line,"%s %[^\n]s",arr,arr2);
//printf("Arr::%s,\tArr2::%s\n",arr,arr2);
arrRows_1stDim[loop] = malloc(sizeof(char) * (strlen(arr) + 1));
arrRows_2ndDim[loop] = malloc(sizeof(char) * (strlen(arr2) + 1));
// check if our malloc() was allocated
if(arrRows_1stDim[loop] == NULL || arrRows_2ndDim[loop] == NULL)
{
fprintf(stderr,"Couldn't malloc()");
return -1;
}
strcpy(arrRows_1stDim[loop],arr);
strcpy(arrRows_2ndDim[loop],arr2);
//printf("arrRows_1stDim[loop]::%s,\tarrRows_2ndDim[loop]::%s\n",arrRows_1stDim[loop],arrRows_2ndDim[loop]);
}
}
printf("\nReading Cols...\n\n");
for(loop=0;loop<Ncol;loop++)
{
fgets(line,LENGTH,fp);
if(strlen(line)==1)
loop--;
else
{
sscanf(line,"%s %[^\n]s",arr,arr2);
//printf("Arr::%s,\tArr2::%s\n",arr,arr2);
arrCols_1stDim[loop] = malloc(sizeof(char) * (strlen(arr) + 1));
arrCols_2ndDim[loop] = malloc(sizeof(char) * (strlen(arr2) + 1));
// check if our malloc() was allocated
if(arrCols_1stDim[loop] == NULL || arrCols_2ndDim[loop] == NULL)
{
fprintf(stderr,"Couldn't malloc()");
return -1;
}
strcpy(arrCols_1stDim[loop],arr);
strcpy(arrCols_2ndDim[loop],arr2);
}
}
printf("Let's see whether we read correctly or not...\n");
printf("Printing Rows...\n");
for(loop=0;loop<Nrow;loop++)
{
printf("Rows_1stArray:: %s,\t Rows_2ndArray:: %s\n",arrRows_1stDim[loop],arrRows_2ndDim[loop]);
}
printf("\nPrinting Cols...\n");
for(loop=0;loop<Ncol;loop++)
{
printf("Rows_1stArray:: %s,\t Rows_2ndArray:: %s\n",arrCols_1stDim[loop],arrCols_2ndDim[loop]);
}
printf("\nFree Allocated Memory...\n");
// free inner level
for(loop=0;loop<Nrow;loop++)
{
free(arrRows_1stDim[loop]);
free(arrRows_2ndDim[loop]);
}
// free outer level
free(arrRows_1stDim);
free(arrRows_2ndDim);
for(loop=0;loop<Ncol;loop++)
{
free(arrCols_1stDim[loop]);
free(arrCols_2ndDim[loop]);
}
free(arrCols_1stDim);
free(arrCols_2ndDim);
fclose(fp);
printf("Done!\n");
return 0;
}

输出::

Nrow::20,       Ncol::20
Reading Rows...


Reading Cols...

Let's see whether we read correctly or not...
Printing Rows...
Rows_1stArray:: 1, Rows_2ndArray:: 2
Rows_1stArray:: 1, Rows_2ndArray:: 2
Rows_1stArray:: 3, Rows_2ndArray:: 4 2 2
Rows_1stArray:: 5, Rows_2ndArray:: 4 1 1 1 1
Rows_1stArray:: 4, Rows_2ndArray:: 2 1 3 1
Rows_1stArray:: 3, Rows_2ndArray:: 2 1 7
Rows_1stArray:: 4, Rows_2ndArray:: 1 3 3 2
Rows_1stArray:: 2, Rows_2ndArray:: 10 1
Rows_1stArray:: 5, Rows_2ndArray:: 8 2 1 1 1
Rows_1stArray:: 3, Rows_2ndArray:: 8 1 1
Rows_1stArray:: 4, Rows_2ndArray:: 9 1 1 1
Rows_1stArray:: 4, Rows_2ndArray:: 3 8 1 1
Rows_1stArray:: 2, Rows_2ndArray:: 2 12
Rows_1stArray:: 4, Rows_2ndArray:: 3 2 4 3
Rows_1stArray:: 4, Rows_2ndArray:: 2 2 4 3
Rows_1stArray:: 3, Rows_2ndArray:: 3 4 2
Rows_1stArray:: 2, Rows_2ndArray:: 3 2
Rows_1stArray:: 3, Rows_2ndArray:: 2 1 2
Rows_1stArray:: 2, Rows_2ndArray:: 5 4
Rows_1stArray:: 2, Rows_2ndArray:: 4 2

Printing Cols...
Rows_1stArray:: 1, Rows_2ndArray:: 6
Rows_1stArray:: 1, Rows_2ndArray:: 9
Rows_1stArray:: 2, Rows_2ndArray:: 9 3
Rows_1stArray:: 4, Rows_2ndArray:: 4 4 1 1
Rows_1stArray:: 3, Rows_2ndArray:: 1 2 8
Rows_1stArray:: 3, Rows_2ndArray:: 4 9 1
Rows_1stArray:: 4, Rows_2ndArray:: 2 7 1 1
Rows_1stArray:: 2, Rows_2ndArray:: 7 1
Rows_1stArray:: 3, Rows_2ndArray:: 1 5 1
Rows_1stArray:: 3, Rows_2ndArray:: 8 5 1
Rows_1stArray:: 4, Rows_2ndArray:: 1 3 7 1
Rows_1stArray:: 5, Rows_2ndArray:: 1 2 2 3 1
Rows_1stArray:: 5, Rows_2ndArray:: 2 1 2 2 1
Rows_1stArray:: 3, Rows_2ndArray:: 2 4 1
Rows_1stArray:: 4, Rows_2ndArray:: 2 1 4 1
Rows_1stArray:: 6, Rows_2ndArray:: 1 1 1 1 2 1
Rows_1stArray:: 4, Rows_2ndArray:: 1 2 1 2
Rows_1stArray:: 3, Rows_2ndArray:: 8 1 2
Rows_1stArray:: 1, Rows_2ndArray:: 4
Rows_1stArray:: 1, Rows_2ndArray:: 2

Free Allocated Memory...
Done!

关于c - 将文件 .txt 读取到数组并拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16285200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com