LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [C] Can't print in screen the content of file (https://www.linuxquestions.org/questions/programming-9/%5Bc%5D-cant-print-in-screen-the-content-of-file-796715/)

netpumber 03-20-2010 01:27 PM

[C] Can't print in screen the content of file
 
why this code can't print in the while loop the content of the file ?

Code:

#include <stdio.h>


int main()
{
        FILE *fp;
        char text;

        fp = fopen("testq.txt","w");
       
        if (fp == NULL){
                printf ("This file doesn't exists or we can't write on it\n");
        }else{
                printf("This file exists and we can write on it\n");
                fprintf(fp,"yooo i can write in you\n");
        }
       
        fprintf(fp,"yoooo maybe this is a second line\n");

        while (fscanf(fp,"%s",&text) != EOF){
              printf("This is what is in the file:\n");
              printf("%s",text);
        }                     
       
        fclose(fp);

return 0;
       

}


Maligree 03-20-2010 02:22 PM

First off, your main problem lies here:
Code:

fp = fopen("testq.txt","w"); /* you're trying to read from a file that's open for writing __only__*/
Second, note that your program will segfault if the file cannot be opened. The line:
Code:

fprintf(fp,"yoooo maybe this is a second line\n");
will be executed even if fopen fails.

The third, major problem is your loop. It won't work correctly written this way. But I don't really want to spoon feed you here, take it for a spin, try to fix it and report back if needed, mkay?

smeezekitty 03-20-2010 02:56 PM

Change
Code:

fp = fopen("testq.txt", "w");
to
Code:

fp = fopen("testq.txt", "r+");
BTW still posting from ReactOs.

netpumber 03-20-2010 03:09 PM

so.. the file testq.txt has chmod 777 so the prog can write to it.

i run

cat testq.txt

and take back what i want

yooo i can write in you
yoooo maybe this is a second line


The problem is why the program doesn't prints the content of this file..

@Maligree : Maybe the prob is in loop as you said.. How about to use feof()

@smeezekitty : Why to do something like that ? And if i do it still doesn't work..

smeezekitty 03-20-2010 03:12 PM

Quote:

Originally Posted by netpumber (Post 3905808)
@smeezekitty : Why to do something like that ? And if i do it still doesn't work..

Because otherwise the file is write-only.

Maligree 03-20-2010 03:22 PM

You're confusing filesystem permissions with those used when opening a file. You're opening it with the "w" flag set - this means "try to open this file for writing only", no matter what the filesystem permissions are.

Screw it, I'm in a good mood today. Take a look:
Code:

#include <stdio.h>
#include <stdlib.h>

int main() {
        FILE *fp;
        char text[128];

        fp = fopen("testq.txt","r+");

        if (fp == NULL){
                printf ("This file doesn't exists or we can't write on it\n");
                exit(1);
        } else {
                printf("This file exists and we can write on it\n");
                fprintf(fp,"yooo i can write in you\n");
                fprintf(fp,"yoooo maybe this is a second line\n");
        }

       

        fseek(fp,0,SEEK_SET);
        printf("This is what is in the file:\n"); // moved
        while (fscanf(fp,"%s",text) != EOF) { printf("%s\n",text); }

        fclose(fp);

        return 0;
}

This __will__ work. Check out what fseek does and try to understand why you need it. Also, you could also prefer to use fread to read the contents, but that really depends on the situation.

netpumber 03-20-2010 03:59 PM

Ok thanks a lot i'll see it..

Sergei Steshenko 03-20-2010 04:50 PM

Quote:

Originally Posted by netpumber (Post 3905856)
Ok thanks a lot i'll see it..

In real life one very rarely needs to read and write to the same file descriptor. So, probably something is wrong with design/architecture.


All times are GMT -5. The time now is 07:58 AM.