fscanf problems

This is a discussion on fscanf problems within the C Programming forums, part of the General Programming Boards category; I'm trying to take text from a file, and have it display in the WIN32 console window(i'm using visual C++ ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    9

    Question fscanf problems

    I'm trying to take text from a file, and have it display in the WIN32 console window(i'm using visual C++ 6.0). I'm getting an "Unhandled exception : Access Violation" error when I try to run the following compiled code, which has no errors or warnings otherwise I obviously couldn't get it to run at all. If anyone knows what i'm doing wrong i'd greatly appreciate some advice. I know this post might be better suited for the C++ board but i figured since i'm using a C library and C functions it would be ok to post it here. Here's the code:



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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > i'm using visual C++ 6.0
    That's just the name of your compiler. If you're writing in C (as you seem to be), then this is the board to post on.

    Its fscanf which returns EOF, not any value of counter. Another key bit is the & in the last parameter to fscanf


    A bit like this
    Code:
    #include <stdio.h>
    
    int main()
    {
        FILE *stream;
        char txtstring[1000];
        int cntr, i;
        
        stream = fopen("tmp2.txt","r");
    
        // not the recommended way, but it uses a for loop    
        for ( cntr=0;
              fscanf(stream,"%c", &txtstring[cntr]) != EOF;
              cntr++ );
    
        for (i=0; i < cntr; i++)
              printf("%c",txtstring[i]);
        
        return 0;
    }

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You should try and not use fscanf when you don't have to. Use fgetc() instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf problems
    By gambit1414 in forum C Programming
    Replies: 1
    Last Post: 04-12-2009, 01:20 PM
  2. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  3. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 01:32 PM
  5. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 06:24 AM

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