C language reads the data of txt file

Reading txt data can be realized in one sentence in matlab, but it is much more complicated in C language. After trying many methods, I finally got the data I wanted. The method is rather stupid, but fortunately, it is only used for experimentation, so you don’t care about efficiency, and you can improve it later when you see a good method.

include “stdafx.h”

include

include

include

include

include

using namespace std;

define LINENUM 12783

struct FRAME
{
int framenum;
int objnum;
int startx;
int starty;
int width;
int height;
} frame[LINENUM];

int _tmain(int argc, _TCHAR* argv[])
{
ofstream fresult(“result.txt”);
int framenum;
int objnum;
float startx;
float starty;
float width;
float height;

string perline;
ifstream input("record_target_trajectory.txt");
for (int num=0;num<LINENUM && getline(input,perline);num++)
{       
    ofstream fout("line.txt");
    fout << perline <<endl;
    FILE *fin;
    fin = fopen("line.txt","r");
    if (fin == NULL)
    {
        printf("fail to open\n");
        exit(1);
    }
    int temp = fscanf(fin,"%d:%d:%f:%f:%f:%f:\n",&framenum,&objnum,&startx,&starty,&width,&height);
    if(temp == 6)
    {
        frame[num].framenum = framenum;
        frame[num].startx = (int)(startx * 1280);
        frame[num].starty = (int)(starty * 720);
        frame[num].width = (int)(width * 1280);
        frame[num].height = (int)(height * 720);
        printf("%d,%d,%d,%d,%d\n",frame[num].framenum,frame[num].startx,frame[num].starty,frame[num].width,frame[num].height);
    }
    fclose(fin);
}
system("pause");
return 0;

}

Method Two

The project needs to process the data in the txt file, the data comes from verilog algorithm simulation.
The example data here is as follows:
11.txt The

fscanf() and fprintf() functions are similar to the scanf() and printf() used earlier. They are both formatting read and write functions. The difference between the two is fscanf() and fprintf() The object of reading and writing is not the keyboard and the monitor, but the disk file.
int fscanf (FILE *fp, char * format,… );
int fprintf (FILE *fp, char * format,… );

fp is the file pointer, format is the format control string, and … represents the parameter list.

FILE *fp;
int i, j;
char *str, ch;
fscanf(fp, “%d %s”, &i, str);
fprintf(fp,”%d %c”, j, ch);
1
2
3
4
5
Sample code:

include

include

include

include

define MAX_LINE 1024

int main()
{
int buf[MAX_LINE];
int i;
FILE *fp=fopen(“11.txt”,”r”);
int len;
if(fp==NULL) {
printf(” Open file Error !\n”);
exit(0);
}

for(i=0;i<10;i++){
fscanf(fp,"%d \n",&buf[i]);

}

printf(“%d\n”,buf[0]);

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Running result:
ubuntu:~/C/CEX/ex7_file_read$ ./change
233441
ubuntu:~/C/CEX/ex7_file_read$
————————————————
版权声明:本文为CSDN博主「LEEE@FPGA」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/baidu_34971492/article/details/120044677

Leave a Reply

Your email address will not be published.

en_USEnglish