-1

I have a program that reads the data from a stream.
Let's just pretend that I already made file in C:\ (record.txt) and I wrote the following words:

hello world
me you

But the program will display in this manner:
hello
world
me
you

instead of:
hello world
me you

"PLEASE HELP ME WITH THIS!!!" here's my code...

#include<stdio.h>
int main()
{
    FILE *fp;
    fp=fopen("c:\\record.txt","r");
    char name[30];
    printf("NAME:\n");
    while(!feof(fp)) {
        fscanf(fp,"%s",&name);
        if(feof(fp))
            break;
        printf("%s\n",name);
    }
    return 0;
}

Please help me!

Edited by Narue: added code tags and formatting

Votes + Comments
please use code tags
2
Contributors
2
Replies
5
Views
6 Years
Discussion Span
Last Post by D.M.
0

Obviously because you're reading each string from the file saving into a variable and printing newline('\n') after each one.

printf("%s\n",name);

Don't you think this :

if(feof(fp))
break;

is unnecessary since the while statement already checks the condition and in addition it's preventing the last string from being printed out.
Also don't forget to close the file after finish using it

fclose(fp);

.

Edited by D33wakar: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.