LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-20-2010, 02:27 PM   #1
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 338

Rep: Reputation: 31
[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;
	

}
 
Old 03-20-2010, 03:22 PM   #2
Maligree
Member
 
Registered: Mar 2008
Distribution: Gentoo, CentOS, Fedora, Arch
Posts: 231
Blog Entries: 1

Rep: Reputation: 42
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?
 
Old 03-20-2010, 03:56 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,234

Rep: Reputation: 189Reputation: 189
Change
Code:
fp = fopen("testq.txt", "w");
to
Code:
fp = fopen("testq.txt", "r+");
BTW still posting from ReactOs.
 
Old 03-20-2010, 04:09 PM   #4
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 338

Original Poster
Rep: Reputation: 31
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..
 
Old 03-20-2010, 04:12 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,234

Rep: Reputation: 189Reputation: 189
Quote:
Originally Posted by netpumber View Post
@smeezekitty : Why to do something like that ? And if i do it still doesn't work..
Because otherwise the file is write-only.
 
Old 03-20-2010, 04:22 PM   #6
Maligree
Member
 
Registered: Mar 2008
Distribution: Gentoo, CentOS, Fedora, Arch
Posts: 231
Blog Entries: 1

Rep: Reputation: 42
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.
 
Old 03-20-2010, 04:59 PM   #7
netpumber
Member
 
Registered: Sep 2007
Location: In My Box
Distribution: Arch Linux
Posts: 338

Original Poster
Rep: Reputation: 31
Ok thanks a lot i'll see it..
 
Old 03-20-2010, 05:50 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by netpumber View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 8 12-18-2013 06:36 PM
awk: how can I print out a message to the screen when redirecting the output to file. quanba Programming 5 03-28-2010 06:25 PM
Print-to-file print driver to print PDF Bill Fox Linux - General 3 05-02-2006 05:15 PM
How to capture the screen content? Turin Turambar Linux - Newbie 5 04-07-2005 06:25 PM
C: How can i print out the content of a file in pages? akin81 Programming 1 03-25-2004 09:46 PM


All times are GMT -5. The time now is 10:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration