Thread: problem in printing

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    problem in printing

    hello
    the following part of the programme returns <null> and i cant see the mistake..
    the file1.txt contains a list of names and last names so it was supposed to print one name at a time.

    Code:
    FILE *source;
    char line[100];
    
    typedef char* type;
    type x;
    
    source = fopen("file1.txt","a+");
    if (source == NULL){
                printf("ERROR!!!file not found!");       }
    while(!feof(source)){
                x = fgets(line,100,source);
                printf("%s\n",x);
            }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Wow, where to even begin on this one? Oh I know, how about at a pet peeve of mine!
    Code:
    typedef char* type;
    I hate people that do this.
    Code:
    source = fopen("file1.txt","a+");source = fopen("file1.txt","a+");
    You are opening in append mode (well a+ technically), and as such, the file pointer is at the end of the file:
    Quote Originally Posted by man fopen
    ``a+'' Open for reading and writing. The file is created if it does not
    exist. The stream is positioned at the end of the file. Subse-
    quent writes to the file will always end up at the then current
    end of file, irrespective of any intervening fseek(3) or similar.
    Code:
    while(!feof(source)){
    There are very few times when you should use feof for loop control. This isn't one of them. Read the FAQ on it.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Printing problem
    By Frandy in forum Windows Programming
    Replies: 0
    Last Post: 12-08-2004, 01:19 PM
  3. Problem Printing
    By LuckY in forum Windows Programming
    Replies: 7
    Last Post: 01-20-2003, 05:01 PM
  4. Printing problem
    By face_master in forum Windows Programming
    Replies: 4
    Last Post: 08-02-2002, 01:04 AM
  5. Printing Problem
    By RubenJ in forum Windows Programming
    Replies: 2
    Last Post: 12-05-2001, 12:27 AM