How to save the contents of a text file into an array of structures

I have a text file A.TXT, as follows
1 XX.DBF 10000000
1 YY.DBF 12000000
2 XXXX.DBF 680000000
2 YYYY.DBF 12345678
2 AA.DBF 55555555
3 BBB.DBF 666666666
3 CCC.DBF 871332

One of the first and third columns are integers
How to read the text file and save the contents of the 3 column to an array of structures.?

Started by Lorin at November 16, 2016 - 10:00 AM

The middle is TAB separated

Posted by Lorin at November 26, 2016 - 10:35 AM

This is read a line
std::ifstream file;
	file.open("f:\\test.txt");
	char buf[1024]={0},buffer[20]={0},tem[20]={0};
	file.getline(buf,sizeof(char[1024]));
	int i=0,j=0,number1,number2;
	while(buf[i]!=9)
	{
		buffer[j++]=buf[i++];
	}
	i++;j=0;
	number1=atoi(buffer);
	memset(buffer,0,sizeof(char[20]));
	while(buf[i]!=9)
	{
		buffer[j++]=buf[i++];
	}
	i++;j=0;
	memcpy(tem,buffer,sizeof(char[20]));
	memset(buffer,0,sizeof(char[20]));
	while(buf[i]!=13)
	{
		buffer[j++]=buf[i++];
	}
	number2=atoi(buffer);

Posted by Deborah at December 04, 2016 - 10:36 AM

fgets
sscanf

Posted by Greta at December 16, 2016 - 11:01 AM

Thank you, I have not used, only C++1 weeks

Posted by Lorin at December 20, 2016 - 11:26 AM

//I have a text file A.TXT, as follows
// 1  XX.DBF     10000000
// 1  YY.DBF     12000000
// 2  XXXX.DBF   680000000
// 2  YYYY.DBF   12345678
// 2  AA.DBF     55555555
// 3  BBB.DBF    666666666
// 3  CCC.DBF    871332
#include <stdio.h>
#define MAXL 100
#define MAXC 40
struct DATA {
    int no;
    char name[13];
    int recnum;
} d[MAXL];
FILE *f;
int i,n;
char ln[MAXC+1];
int main() {
    f=fopen("A.TXT","r");
    if (NULL==f) {
        printf("Can not open file A.TXT!\n");
        return 1;
    }
    i=0;
    n=0;
    while (1) {
        if (NULL==fgets(ln,MAXC,f)) break;
        n++;
        if (3==sscanf(ln,"%d%12s%d",&d[i].no,d[i].name,&d[i].recnum)) {
            i++;
            if (i>=MAXL) {
                printf("Too many datas(>%d datas) ignored.\n",MAXL);
                break;
            }
        } else {
            printf("Line %d format error:%s",n,ln);
        }
    }
    fclose(f);
    n=i;
    for (i=0;i<n;i++) {
        printf("%d %-12s %d\n",d[i].no,d[i].name,d[i].recnum);
    }
    return 0;
}
//1 XX.DBF       10000000
//1 YY.DBF       12000000
//2 XXXX.DBF     680000000
//2 YYYY.DBF     12345678
//2 AA.DBF       55555555
//3 BBB.DBF      666666666
//3 CCC.DBF      871332
//

Posted by Greta at December 24, 2016 - 11:41 AM

Thank you, good complex, getline can not go in there.?

Posted by Lorin at January 05, 2017 - 12:37 PM

The code that called getline. Try it
1  XXXXXXXXXXXXXXXXXXXXXX.DBF     10000000
This is a row of garbage
1  YY.DBF     12000000
2  XXXX.DBF   680000000
2  YYYY.DBF   12345678
2  AA.DBF     55555555
3  BBB.DBF    666666666
3  CCC.DBF    871332
...
(more than the maximum number of rows such as line 10000000000)

Posted by Greta at January 08, 2017 - 1:23 PM

Certainly not so many lines, up to 10000.

Posted by Lorin at January 11, 2017 - 2:06 PM

Zhao Laoguo really domineering

Posted by Zebulon at January 14, 2017 - 2:51 PM