读取一个txt文件,并在C中记忆为int或long
我不是天才。所以…首先,我为我愚蠢的问题感到抱歉。 我有一个.txt文件或.dta(随便什么),我想用C读它。 我想将txt的数字格式化为两列和x行,并将所有数据存储到矩阵(或向量)中 据我所知,我正在使用此代码:读取一个txt文件,并在C中记忆为int或long,c,file-io,matrix,C,File Io,Matrix,我不是天才。所以…首先,我为我愚蠢的问题感到抱歉。 我有一个.txt文件或.dta(随便什么),我想用C读它。 我想将txt的数字格式化为两列和x行,并将所有数据存储到矩阵(或向量)中 据我所知,我正在使用此代码: /* ** File FILE_3.C ** ** Illustrates how to read from a file. ** ** The file is opened for reading. Each line is successively fetched ** usi
/*
** File FILE_3.C
**
** Illustrates how to read from a file.
**
** The file is opened for reading. Each line is successively fetched
** using fgets command. The string is then converted to a long integer.
**
** Note that fgets returns NULL when there are no more lines in the file.
**
** In this example file ELAPSED.DTA consists of various elapsed times in
** seconds. This may have been the result of logging the time of events
** using an elapsed time counter which increments each second from the
** time the data logger was placed in service.
**
** Typical data in elapsed.dta might be;
**
** 653 75
** 142 90
** 104 10
** 604 10
** 124 12
**
*/
#include <stdio.h> /* required for file operations */
FILE *fr; /* declare the file pointer */
main()
{
int n;
long elapsed_seconds;
char line[80];
fr = fopen ("elapsed.txt", "rt"); /* open the file for reading */
/* elapsed.dta is the name of the file */
/* "rt" means open the file for reading text */
while(fgets(line, 80, fr) != NULL)
{
sscanf (line, "%ld", &elapsed_seconds);
/* convert the string to a long int */
printf ("%ld\n", elapsed_seconds);
}
fclose(fr); /* close the file prior to exiting the routine */
} /*of main*/
/*
**文件3.C
**
**说明如何从文件中读取。
**
**文件已打开以供读取。每一行都是连续提取的
**使用fgets命令。然后将字符串转换为长整数。
**
**请注意,当文件中没有更多行时,fgets返回NULL。
**
**在本例中,文件eassed.DTA由中的各种运行时间组成
**秒。这可能是记录事件时间的结果
**使用每秒从
**数据记录器投入使用的时间。
**
**explosed.dta中的典型数据可能是:;
**
** 653 75
** 142 90
** 104 10
** 604 10
** 124 12
**
*/
#包含文件操作所需的/**/
文件*fr;/*声明文件指针*/
main()
{
int n;
长时间运行的秒数;
字符行[80];
fr=fopen(“appead.txt”、“rt”);/*打开文件进行读取*/
/*appeased.dta是文件名*/
/*“rt”表示打开文件以读取文本*/
while(fgets(第80行,fr)!=NULL)
{
sscanf(行、%ld、&U秒);
/*将字符串转换为长整型*/
printf(“%ld\n”,已用\u秒);
}
fclose(fr);/*退出例程前关闭文件*/
}/*主要*/
你能帮我吗
感谢您的回答您必须分配一个数组来保存已用秒数
long * seconds_array;
seconds_array = calloc( SIZEOFARRAY, sizeof(long));
在c语言中,数组与指针大致相同
就像这个例子中的
在本例中,pData的大小是固定的,这意味着您必须事先知道自己有多少个数字,或者必须跟踪输入的数字,并在空间不足时分配新的pData数组
在本例中,数字取自标准DIN,但原理相同。当从文件中读取时。在您的例子中,for循环应该更像while循环
/* calloc example */
#include <stdio.h> /* printf, scanf, NULL */
#include <stdlib.h> /* calloc, exit, free */
int main ()
{
int i,n;
int * pData;
printf ("Amount of numbers to be entered: ");
scanf ("%d",&i);
pData = (int*) calloc (i,sizeof(int));
if (pData==NULL) exit (1);
for (n=0;n<i;n++)
{
printf ("Enter number #%d: ",n+1);
scanf ("%d",&pData[n]);
}
printf ("You have entered: ");
for (n=0;n<i;n++) printf ("%d ",pData[n]);
free (pData);
return 0;
}
/*calloc示例*/
#include/*printf、scanf、NULL*/
#包括/*calloc、退出、免费*/
int main()
{
inti,n;
int*pData;
printf(“要输入的数字量:”);
scanf(“%d”、&i);
pData=(int*)calloc(i,sizeof(int));
如果(pData==NULL)退出(1);
对于(n=0;n您可以使用fscanf,而不是fgets和sscanf。
仔细看看scanf格式()
#包括
#包括
int main()
{
文件*fr;
int行=0;
int i;
int arr[8][2];//最多8行!
fr=fopen(“file.txt”,“r”);
如果(fr==NULL)
{
printf(“无法打开文件\n”);
出口(0);
}
而(fscanf(fr,“%d%d\n”、&arr[row][0]、&arr[row][1])==2)
行++;
对于(i=0;i|
main()
前面没有int
是1985年开始的C编程。也许是时候更新这个例子了。问题是什么?只要使用sscanf它只阅读第一列,那么就发布你的代码,而不是一些与你的问题毫无共同之处的例子。这对你来说怎么不起作用?
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * fr;
int row = 0;
int i;
int arr[8][2]; // max 8 rows!
fr = fopen ("file.txt", "r");
if ( fr == NULL )
{
printf( "Can't open file\n" );
exit(0);
}
while( fscanf (fr, "%d %d\n", &arr[row][0], &arr[row][1]) == 2 )
row ++;
for (i = 0; i < row; i++)
printf( "(%d) (%d)\n", arr[i][0], arr[i][1] );
fclose(fr);
}