Can someone find out what i'm doing wrong?

This is a discussion on Can someone find out what i'm doing wrong? within the C Programming forums, part of the General Programming Boards category; Hi, I posted a very similar program to this one recently, but i'm still having trouble getting it to work. ...

  1. #1
    QuincyEQ
    Guest

    Question Can someone find out what i'm doing wrong?

    Hi, I posted a very similar program to this one recently, but i'm still having trouble getting it to work. What i want to do is simply take text from a .txt file and have its contents copied to the console window. Now what's really baffling me is when I run this program in the debugger(MSVC++ 6.0), it tells me fscanf, and printf return a 1, and my if statement says if the function doesn't return a -1 to go ahead and process the next statement. Yet for some reason it skips the statements and only goes through the functions once. This is such a simple program and i'm still baffled. Well here's the code, if anyone knows what the problem is i'd greatly appreciate some help.

    Code:
    #include <stdio.h>
    FILE *stream;
    
    int main()
    {
    	char txtstring[1000];
    	int cntr = 0;
    	
    	stream = fopen("teachers.txt","r");
    	
    	if( fscanf(stream,"%c",&txtstring[cntr]) != -1)
    		cntr++;
    
    	cntr = 0;
    	
    	if( printf("%c",txtstring[cntr]) != -1)
    		cntr++;
    
    	return 0;
    }

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    If you want to print the whole .txt file you must have the read (fscanf) function in a loop.

    EDIT ...

    Here's the example:
    Code:
    FILE *fin = NULL;
    char character;
    
    fin= fopen ("teachers.txt","r");
    while ((fscanf (fin, "%c", &character)) != EOF)
    {
        printf("%c", character);
    }
    fclose (fin);
    Last edited by GaPe; 08-15-2002 at 11:07 AM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    QuincyEQ
    Guest

    Smile Thanks

    Thanks, that solved the problem. I can finally move away from this program and on to bigger and better things. lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linux find utilty
    By vaibhavs17 in forum Tech Board
    Replies: 5
    Last Post: 05-12-2009, 04:40 AM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. Help!!!!!!!! What Am I Doing Wrong????
    By Cnote in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2002, 10:09 PM
  4. Help i cant find whats wrong with my code
    By 0927 in forum C++ Programming
    Replies: 1
    Last Post: 12-21-2001, 08:14 PM
  5. Help me find out what's wrong
    By biosx in forum C Programming
    Replies: 4
    Last Post: 08-15-2001, 12:37 PM

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