Home > Back-end >  C language to read the file
C language to read the file

Time:12-05

I create an array "sequence", he could read from the storage structure. TXT "number after each of the router. The router number for" nb_input ", we can choose the number range is "nb_output".
For example, if the TXT file like this:
Router1 1, 3, 4,
In the sequence of the corresponding position will be assigned to 1=& gt; The sequence [0] [0]=1, the sequence [0], [2]=1 sequence [0] [3]=1
My "structure. TXT" goes like this:

My code is as follows. Nb_input=6, nb_output=16 has been pre-defined
 
/* read structure. TXT */
#include
#include
#include
#include
#include
# define BUFFER_SIZE 1024
# define WHITESPACE 0 x20
# define TAB 0 x09
Int main ()
{
The FILE * fStruct, * fPara;
Char line [BUFFER_SIZE];
Int I=0, j=0;
Char parameters_name [BUFFER_SIZE];
Char parameters_value [BUFFER_SIZE];
Int nb_input;
Int nb_output;
Int sum_size;
Int b;
Int c;
Int l=0;
Char s [10];
Int k=0;
Int start=0;
Int sequence [nb_input] [nb_output];
/* inisalisation of sequence array */
For (I=0; i{
For (j=0; J{
The sequence [I] [j]=0;
}
}
FStruct=fopen (" C:/Users/13383/Desktop/creater_noc/structure. TXT ", "rt");
If (fStruct==NULL)
{
Printf (" ERROR: Can 't open the file. ");
exit(1);
}
Int r;
While (the fgets (line, BUFFER_SIZE, fStruct)!=NULL)//Read a line
{
I=0, j=0, l=0, start=0, c=0, b=0, k=0;
Memset (s, 0, sizeof (s));

While (line [I]==WHITESPACE | | line [I]==TAB)//Skip Spaces or Tabs at the beginning of this line
{
i++;
}
If (line [I]=='\ n')//Skip Blank lines
{
continue;
}
If (line [I]=='#')//The comments lines begin with a #
{
i++;
}
The else
{
For (I=6; Line [I]!='\ n' & amp; & Line [I]!=1; I++)
{

If (line [I] <='9' & amp; & Line [I] & gt;='0')
{
If (start==1 & amp; & K==0)
{
B=atoi (s);
Memset (s, 0, sizeof (s));
}
Else if (start==2 & amp; & K==0)
{
C=atoi (s);
Memset (s, 0, sizeof (s));
If (b>=1 & amp; & C>
=1){
Sequence [1 b] [c - 1]=1;
}
//printf (" b=% d, c=% d \ n ", b, c);
}
S [k]=line [I];
for(; The line [I + 1] <='9' & amp; & Line [I + 1] & gt;='0')
{
i++;
K++;
S [k]=line [I];
}
If (line [I] - k - 1=='r')
{
Start=1;
}
Else if (line [I + 1)=='\ n')
{
C=atoi (s);
Memset (s, 0, sizeof (s));
If (b>=1 & amp; & C>
=1){
Sequence [1 b] [c - 1]=1;
}
//printf (" b=% d, c=% d \ n ", b, c);
continue;
}
Else if (line [I + 1)==(EOF)
{
C=atoi (s);
Memset (s, 0, sizeof (s));
If (b>=1 & amp; & C>
=1){
Sequence [1 b] [c - 1]=1;
}
//printf (" b=% d, c=% d \ n ", b, c);
continue;
}
The else
{
Start=2;
}
//k++;

}
Else if (start==1 | | start==2)
{
K=0;
}
}
}
}
}

I found that I always read the last line is wrong, may be because of EOF, excuse me, everyone is my code problem?

CodePudding user response:

 else if (line [I + 1)==EOF | | line [I + 1)=='\ 0')//add a is equal to '\ 0' judgment 

The end of the file may be '\ 0'
C + + environment debugging VS2015

 # include & lt; stdio.h> 
#include
#include
#include
#include
# define BUFFER_SIZE 1024
# define WHITESPACE 0 x20
# define TAB 0 x09

Int main ()
{
The FILE * fStruct=nullptr, * fPara=nullptr;
Char line [BUFFER_SIZE];
Int I=0, j=0;
Char parameters_name [BUFFER_SIZE]={0};
Char parameters_value [BUFFER_SIZE]={0};
Const int nb_input=6;
Const int nb_output=16;
Int sum_size=0;
Int b;
Int c;
Int l=0;
Char s [10];
Int k=0;
Int start=0;
Int sequence [nb_input] [nb_output];
/* inisalisation of sequence array */
For (I=0; i{
For (j=0; J{
The sequence [I] [j]=0;
}
}
FStruct=fopen (" d: \ \ structure. TXT ", "rt");
If (fStruct==NULL)
{
Printf (" ERROR: Can 't open the file. ");
exit(1);
}
Int r=0;
While (the fgets (line, BUFFER_SIZE, fStruct)!=NULL)//Read a line
{
I=0, j=0, l=0, start=0, c=0, b=0, k=0;
Memset (s, 0, sizeof (s));

While (line [I]==WHITESPACE | | line [I]==TAB)//Skip Spaces or Tabs at the beginning of this line
{
i++;
}
If (line [I]=='\ n')//Skip Blank lines
{
continue;
}
If (line [I]=='#')//The comments lines begin with a #
{
i++;
}
The else
{
For (I=6; Line [I]!='\ n' & amp; & Line [I]!=1; I++)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related