Home > Software engineering >  Why is my a.t xt file only two Numbers 1, 2, and array type has three members [0]=1; Array [1]=2; Ar
Why is my a.t xt file only two Numbers 1, 2, and array type has three members [0]=1; Array [1]=2; Ar

Time:12-30

 int k, j; 
Int I=0;
An int array [10]={0};
Char [100] s={0};
The FILE * p=fopen (" D: \ \ a.t xt ", "r");
//FILE * p1=fopen (" D: \ \ b.t xt ", "w");
while (! The feof (p))
{
Memset (s, 0, sizeof (s));
The fgets (s, sizeof (s), p);
Array [I]=atoi (s);
Printf (" array [] % d=% d \ n ", I, array [I]);
i++;
}

CodePudding user response:

Pay attention to the file at the back of the blank line!

while (! The feof (p))
{
The fgets (s, sizeof (s), p);
If (s [0]==10)
continue;
Array [I]=atoi (s);
TRACE2 (" array [] % d=% d \ n ", I, array [I]);
i++;
}

CodePudding user response:

An int array [10]={0} you are initialized to 0,
Their Debug step order F10 to perform it and see the while (feof (p)) performed several times was clear to see,

CodePudding user response:

Do not use
While (conditions) 
Don't use more
While (portfolio)
To use the
While (1) {
Condition of an if (1) break;
//...
If conditions (2) continue;
//...
If conditions (3) return;
//...
}
Because the first two kinds of writing on the level of language to express meaning is ambiguous, is only the third faithfully reflect the actual situation of program flow,
Typical such as:
The following two paragraphs semantics are as at the end of the file not read characters
while (! The feof (f)) {
A=fgetc (f);
//...
B=fgetc (f);//may already feof!
//...
}
And write no problem like this:
While (1) {
A=fgetc (f);
If (feof (f)) break;
//...
B=fgetc (f);
If (feof (f)) break;
//...
}
Similar examples can also take a lot of,


"More a less a" problem accounts for more than 10% of programmers often make mistakes!
Avoid "more a less a" problem is one of the methods of will like & lt; 10 even & lt; 5 generations into the program fragment, snapping fingers mental arithmetic test program should write to
X, x - 1, which of the x + 1?
<, & lt;===, & gt; , & gt; Which of the=?