0

I'm writing a simple program which takes data about people in this format: name,age,gender,info

and it will display them like this [name: , age: , gender: , info: ]

Here is my code so far:

#include <stdio.h> int main() { char name[10]; int age; char gender[2]; char info[50]; while(scanf("%9[^,],%i,%c,%49[^\n]", name, &age, gender, info) == 4) { printf("[name: %s, age: %i, gender: %c, info: %s]\n", name, age, gender, info); } return 0; 

}

So I decided to write my output to another text file using >. And it does nto display correctly, the ] bracket displays on a new line and [name: by itself.

This is my input:

 eliza,7,F,likes animals bob,9,M,fast at running sue,6,F,likes painting 

And the output is:

 [name: eliza, age: 7, gender: J, info: likes animals ] [name: bob, age: 9, gender: J, info: fast at running ] [name: sue, age: 6, gender: J, info: likes painting ] 

Can someone help? I can't figure out why it prints the data like this, I tried using strstr() to check if any of my variables contained the new line character.

9
  • I guess this is Windows? Commented Jun 9, 2013 at 16:55
  • Yes, does that affect anything? Commented Jun 9, 2013 at 16:56
  • 2
    Yes; line endings in Windows are \r\n not just \n Commented Jun 9, 2013 at 16:56
  • 1
    @Dave , you are right ..... was working on solaris. +1 for the answer. Commented Jun 9, 2013 at 17:10
  • 1
    I tested thsi on Windows, and I don't get the new line at the end of info. To the OP: what compiler are you using, and how do you enter the input? Commented Jun 9, 2013 at 17:40

2 Answers 2

3

You have two issues. Firstly, I believe this is windows (or at least the file you're reading was created in Windows), which means you have \r\n not just \n at line endings. You can fix that by opening the file in text mode but that's unreliable; it's better to filter the extra \r out manually.

That's what puts a newline after each "info" field.

The second problem is that you reject the \n from the info field, so it's still there as the first character for later "name" fields, which is why you have the extra line break there. To fix it, just put a space at the start of your scanf string (which will swallow any and all whitespace)

And Inspired points out your third issue (which I hadn't noticed); you need to treat gender as a character not a string. The "correct" way to read it is like this:

char gender; // no need to have an array of characters scanf( "blah %c blah", &gender ); 

and print like:

printf( "blah %c blah", gender ); 
Sign up to request clarification or add additional context in comments.

7 Comments

Sorry I'm kind of confused. Right now I have the scanf like this: scanf(" %9[^,],%i,%c,%49[^\r\n]", name, &age, &gender, info) You said I can choose to just filter out \r, but I'm deciding to filter out both \r\n. But if filter at BOTH, I seem to be needing a space after the first string, why? Shouldn't the new line character be gone since I'm filtering both?
@miniJavaLearner no they are separate problems; because your info field rejects the newlines, they are kept in the stream for the next call to read. That's why you also need a space at the start or end of the string to explicitly swallow it.
The code reads from standard input. This immediately means that there's no such thing as \r\n even under Windows. There's no OS sensitvity in that regard whatsoever. All input lines will end in \n. Also, there's absolutely nothing "unreliable" about reading text files through text mode. The problem with \n being left unread is noted corretly. And the problem with %c and char array is indeed another issue.
@AndreyT good point. In that case can you explain the newline at the end of "info"? (at a guess I'd say it's actually reading from somewhere else; does the \r\n conversion apply if the input is piped from another process?)
@Dave: I can't explain it. A quick test on Windows does not reproduce that new-line at the end of info. Could it be that the OP was using a file as input, which somehow contained two newlines at the end of each input line?
|
0
#include <stdio.h> int main(void) { char name[10]; int age; char gender;//change char info[50]; while(scanf("%9[^,],%i,%c,%49[^\n]%*c", name, &age, &gender, info) == 4) { printf("[name: %s, age: %i, gender: %c, info: %s]\n", name, age, gender, info); } return 0; } 

7 Comments

This will leave the new-line at the end of info because the \r is still there.
@Dukeling at windows \r\n convert(I/O:in \r\n ->\n, out \n -> \r\n) to \n. (mode not binary)
@BLUEPIXY non-binary mode is not a reliable way to do that filtering. For example, when running on linux but with a file generated on windows.
@Dukeling: There no \r there, unles sthe OP is using a broken library implementation. All text stream input in C guarantees pure \n line endings.
@Dave: Well, this is simply becuse POSIX does not have non-binary modes at all. It is true that Windows file on Linux will not work properly. But this is certainly not the issue in this case. Moreover, \r, as you probably know, does not produce a new line. It does carriage return without skipping to the next line.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.