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!

D33wakar commented: please use code tags -1

Recommended Answers

All 2 Replies

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);

.

What am I gonna do?
What function should I use?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.