Home > Software engineering >  File. The open open TXT file into an array
File. The open open TXT file into an array

Time:10-10

The great god, help me, with the following code browsing and open the picture in the TXT file, how to placed into a n rows and eight columns of the array, trouble the great god help to add a piece of code, very grateful,
 cstrings filename.//open the file name string 
CFile file;//file object
DWORD len.//used to save the file length
CFileDialog DLG (TRUE, _T (" "), _T (" *. * "), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T (" *. * | *. * | "));

DWORD g2Local_1=DLG. DoModal ();

If (IDOK==g2Local_1) {

Filename. The Format (" % s ", DLG. GetPathName ());
}
Else if (IDCANCEL==g2Local_1) {
return ;
}

//read the data
File. The Open (filename, CFile: : modeReadWrite);
Len=file. GetLength ();
Char * pReadData=https://bbs.csdn.net/topics/new char (len);//allocate memory
File. Read (pReadData, len);//of the data read the file
File. The Close ();

CodePudding user response:

1, each line according to the format to read better, such as the fscanf;
2, define a structure, the number of columns for each row;
3, define the structure dynamic array, save the data,

CodePudding user response:

Fyi:
//NAME: essaie bla bla 
//DIMENSION: 8
//DATA
14 15//1
//2 November 10
//3 6 4
//4 7 13
//5 September 21
//6 19 3
//7 1 5
//8 8 8
//EOF
//
//text file may also contain other content, but the content of the need to use the above

//such as data. TXT:
//NAME: essaie bla bla
//other content
//DIMENSION: 8
//other content
//DATA
//other content
14 15//1
//other content
//2 November 10
//other content
//3 6 4
//other content
//4 7 13
//other content
//5 September 21
//other content
//6 19 3
//other content
//7 1 5
//other content
//8 8 8
//other content
//EOF

//goal is to get the NAME string, after after DIMENSION values, and the DATA of the following numerical
//which NAME is literally a words, DIMENSION is a number of cities, the DATA below is city number, coordinates X, Y coordinates
//all of these will be assigned to a previously defined structure
#include
#include
# define MAXCPL/80/maximum number of characters per line
100//# define MAXCITY DATA in each set of DATA the multiple number, DIMENSION of the maximum
32//# define MAXNAMEL NAME maximum length
Struct S {
Char NAME [MAXNAMEL + 1];
Int DIMENSION;
Struct D {
Int NO;
Int X;
Int Y;
} DATA [MAXCITY];
} s;
The FILE * f;
Int st, n, I;
Char ln [MAXCPL];
Int main () {
F=fopen (" data. TXT ", "r");
If (NULL==f) {
Printf (" Can not open the file data. TXT! \n");
return 1;
}
St=0;
n=0;
While (1) {
If (NULL==the fgets (ln, MAXCPL, f)) break;
If (st==0) {
If (1==sscanf (ln, "NAME: % 31 [^ \ n]", s.N AME)) st=1;
} else if (st==1) {
If (1==sscanf (ln, "DIMENSION: % d", & amp; Spyware doctor IMENSION st=2));
} else if (st==2) {
If (0==STRCMP (ln, "DATA \ n")) st=3;
{} else if (st==3)
If (3==sscanf (ln, "% d % d % d", & amp; Spyware doctor ATA [n]. NO, & amp; Spyware doctor ATA [n]. X, & amp; Spyware doctor ATA [n]. Y)) {
n++;
If (n>=MAXCITY | | n>=s.d. IMENSION) break;
}
}
}
The fclose (f);
Printf (" s.N AME=] [% s \ n ", s.N AME);
Printf (" s.d. IMENSION=% d \ n ", s.d. IMENSION);
For (I=0; iPrintf (" spyware doctor ATA [% d] NO, X, Y=% d, % d, % d \ n ", I, s.d. ATA [I] NO, s.d. ATA [I] X, s.d. ATA [I] Y);
}
return 0;
}
//s.N AME=[essaie bla bla]
//spyware doctor IMENSION=8
//spyware doctor ATA [0]. NO, X, Y=1,14,15
//spyware doctor ATA [1]. NO, X, Y=2,11,10
//spyware doctor ATA [2]. NO, X, Y=3,6,4
//spyware doctor ATA [3]. NO, X, Y=4,7,13
//spyware doctor ATA [4]. NO, X, Y=5,9,21
//spyware doctor ATA [5]. NO, X, Y=6,19,3
//spyware doctor ATA [6]. NO, X, Y=7,1,5
//spyware doctor ATA [7]. NO, X, Y=8,8,8

CodePudding user response:

You read come in to do string segmentation, such as the data can be separated with Spaces

CodePudding user response:

Data. TXT read
Cstrings szLine;
int index=0;
Float fval1,... Fval7=0.0;
File. The ReadString (szLine);
Sscanf (szLine LPCTSTR, _T (" % d % f, % f % f % f % f % f % f "),
& Indxe, & amp; FVal1, & amp; FVal2, & amp; FVal3, & amp; FVal4, & amp; FVal5, & amp; FVal6, & amp; FVal7);
Data. TXT save
Sprintf (szLine LPCTSTR, "% 20-6-13 f % d % % - 19 f % f - 16-8 f % % f - 6-10 f % f",//- behind the Numbers used to align according to their own need to modify the
Indxe fVal1, fVal2 fVal3, fVal4, fVal5, fVal6, fVal7,);
File. Wrtie (szLine);


Manual writing only provides ideas
  • Related