how does reading a txt really works

Ok, so I we learned in school a while ago to read a file and the teacher said it must be done like in the next example:

numbers.txt contains the following:

12 7
124 43 53 0 6 48 3485 237 359 111 593 10
99 44 52 28 0 21 10

reading and displaying numbers.txt

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
29
include <iostream>
#include <stdio.h>
using namespace std;
main ()
{
    FILE* f;
    f=fopen("numbers.txt", "r");

 int n,m,x,v[20],i=1,u[20],j=1;

fscanf(f,"%d %d",&m,&n);

z=m;

while(z!=0)
{fscanf(f,"\n %d ",&x); /*this is what I don't understand, at school we were told that we must put "\n" 
so that the compiler gets on the next line and also the space after "%d" because the numbers 
in the file are parted by 1 space - and today I completely erased the separators (\n, space, 
I even made m++ to see if the result is different) but the display result remained the same */
v[i]=x; i++; z--;
}

z=n;

while(z!=0)
{
    fscanf(f,"\n \n %d ",&x); //these too (\n \n ' ')
    u[j]=x; j++; z--;
}


So changing the code into
1
2
3
4
5
6
7
8
9
10
while(z!=0)
{fscanf(f,"%d",&x);
v[i]=x; i++; z--;
}
z=n;
while(z!=0)
{
    fscanf(f,"%d",&x);
    u[j]=x; j++; z--;
}

haven't changed the result one bit. It still printed:
12 7
124 43 53 0 6 48 3485 237 359 111 593 10
99 44 52 28 0 21 10

So my question is what are the principles of reading a text file, how do variables know when to start reading and when to stop.
In the case of your numbers they are delimited by whitespace. When fscanf hits whitespace it stops and takes the characters already read.
Make every third value in your text file a single letter (also delimited by whitespace.) Adjust your code to parse the file correctly.

While the format they say you should use for this specific case isn't necessary, it will make more sense once you've had a little more experience using it with differently formatted files. Btw, I hope this isn't a C++ class.



You mean like, C++ course class at school? Yes it is, why?
Because it is the most atrocious piece of code I've seen in the last few weeks.
It is incorrect, no standard-compliant compiler will compile it. And it has little to do with C++. It's C and really shitty C. Also, despite what your teacher might have told you, variable names can and should be longer than one character.
You're better off dropping that class and learning it properly.
Last edited on
You made me feel like I created a monster...

Honestly, I don't know why it's wrong, codeblocks runs it.(although I admit it's not pretty) I can't drop that class because it's a high school class, and even if I did, there is no other teacher to learn from.

Regarding this problem particularly, I don't know a way to read numbers form a .txt file, but with the help of repetitive functions, it's just the way we were shown.
So I found http://www.cplusplus.com/doc/tutorial/files/

But I can't use it at the BAC exam, because we didn't learn it in high school, so I'll have to use the old C language with scanf, printf etc. Looks like our school system hasn't been updated for ages.
Topic archived. No new replies allowed.