Thread: Reading a text file and printing it out

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Reading a text file and printing it out

    Hi I am a new drain on the community ready to annoy the real programers.

    I am wondering how to read a text file and then afterwards print it out. I am a bit lost, please be gentle with me.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well... give it your best shot then, when you get stuck, post what you've got and we'll see what we can do...

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are several ways - word by word, char by char, line by line, etc.

    Best if you started out with some tutorials, and worked your way up, on general C programming. Lots of C tutorials, but maybe start with this one:

    Cprogramming.com: How to Get Started with C++

    and Welcome to the forum, Roisis!

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Solution

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[])
    {
    	FILE *f;
    	char s[1000];
    	
    	f=fopen("/Users/rolsis/Desktop/mytext.txt", "r");
    	if(!f)
    		return 1;
    	while (fgets(s,1000,f)!=NULL) {
    		printf("%s", s);
    	}
    	fclose(f);
    	return 0;
    }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And you needed our help.... because?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading and Printing a text file
    By Mikahcho in forum C Programming
    Replies: 20
    Last Post: 12-06-2010, 03:46 PM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. printing arrays, reading from text file
    By joeman in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2010, 02:08 PM
  4. Reading and printing a file
    By T1m in forum C Programming
    Replies: 1
    Last Post: 01-08-2009, 01:29 PM
  5. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM