How to print text from a file to the screen

This is a discussion on How to print text from a file to the screen within the C Programming forums, part of the General Programming Boards category; Hi everyone, I've been fiddling through some tutorials about printing the contents of a file to the screen but when ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    13

    How to print text from a file to the screen

    Hi everyone,

    I've been fiddling through some tutorials about printing the contents of a file to the screen but when I try using commands they get stuck.

    I put this code in but it wont have it

    FILE *fp;
    fp=fopen("file1.txt","r");
    fprintf(fp,"Contents of the file:\n");

    The file1.txt is stored on a server and im using unix terminal.

    I don't get a compiler error but the contents will not appear on screen.

    It is in a program that works without printing the file contents so I have included everything else necessary I just need to correct these lines of code!


    Thanks for the help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That is exactly what you want. You are not printing anything to the screen, so you will see nothing on the screen. If you want to see something, you should print something to the screen instead of to a file.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    13
    Yeh I'm trying to take the contents of the file and print them to the screen. What commands do I need to use instead?

    The reason why I have done this is I have drawn a diagram using symbols in a text file and if I insert them into the program code it will cause a huge mess.
    Cheers

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    fprintf(), IIRC, writes everything to a file. If you want to read from a file, I believe you want to use fgets().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21