Reading a line with a space in start

Hello Everybody.

I have a file like this:
1
2
3
4
5
7
xASDFG
yQWERT
zPOIUYTRE
...


And I use this code to read this file:

1
2
3
4
5
6
7
8
9
10
11
12
13
	FILE *arquivo = fopen("teste.txt", "rb");

	int c;
	char a, b[10];
	fscanf(arquivo,"%d\n", &c);

        printf("%d\n",c);
        while ( !feof(arquivo) )
        {
	     fscanf(arquivo,"%c%s\n",&a, b);
	     printf("%c - %s\n",a,b);
        }
}



I works perfectly, but if I change x on "xASDFG" to a white space character like " ASDFG", the white space is not read.

I've tried use this: fscanf(arquivo,"%c%s\n",&a,b); but have same result.
I've tried use this: fscanf(arquivo,"%[^\n]",b); and then split the b string in 2 parts ( first with 1st char and other with rest of the text), but not read white space too.

Some help?

Thanks :P
Last edited on
So, we read a number and linefeed. Read a character on the new line, then read a string on the same line.

This is what I read in your code. Is that right?

do we know about structures?
Sorry Azagaros, I can't understand what you ask with "do we know about structures?"
keyword struct. I guess I would read the stuff into a general buffer and parse.
Looks like you might have to use fread
You have changed the problem from the original problem this is the fixed problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        FILE *arquivo = fopen("teste.txt", "rb");

	int c;
	char a, b[10];
// fscan will on read the information into this and you don't need to worry about the \n's
// except in your printf's
	fscanf(arquivo,"%d", &c);

        printf("%d\n",c);
        for(int count = 0; count < c; count++)
        {
	     fscanf(arquivo,"%s", b);
	     
             printf("%c\t",b[0]);
             printf("%s\n", *b[1]);        
        }


It seems you need to learn how to use arrays.
Last edited on
Shacktar, I tried to use the fread function to read the first character of the line (blank space).

Used as follows: fread (&a, sizeof (char), 1, file);

But it still did not work.

I also realized that if the blank is not the first character of the line fscanf finds as it should.

Azagaros, sorry to have changed the original problem, was just trying to approach the real problem.

I tried using your solution, but it didn't work.

In line 15, I realized that you're using * instead of &, which is a mistake because you want the address of b[1] and not the value pointed by b[1].

I do not understand why you recommended me to study arrays... Did I make a mistake?

Thanks for your attention.
First thing I would do is stop opening a text file in binary mode.
I apologize for my flub its been a while since I did strait c to get things done. I do heavy c-string manipulations in a lot of things of old code but I convert to a pointer do pointer math. A lot of the c++ programmers out here didn't program before 98.

I do agree with cire that the file doesn't need to be opened as binary. Your reads would match the form of the open. In other words, you wouldn't be using fscan and you would be using exclusively freads and expecting to get everything plus the end of line characters. It would also require a little stronger thought of file pointer manipulation.
Topic archived. No new replies allowed.