Thread: help on reading file

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    18

    help on reading file

    I have the following but doesn't work.... why?
    Code:
    /*
    #include<stdio.h>
    void main(void)
    {
    	FILE *infile;
    	char* line;
    
    	if((infile = fopen("info.txt", "r"))==NULL )
    	{
    		fprintf(stderr, "Error: Cannot open input file. ");
    		exit(1);
    	}
    
    	while(fscanf(infile, "%s", line) != EOF)
    		printf("%s\n", line); 
    }
    */
    Last edited by unhwan; 06-26-2002 at 03:41 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't work because you haven't allocated any space for your pointers. fscanf does not allocate memory.

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

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    void main.... don't do it!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include<stdio.h>
    /* If you are going to use exit(), include
    ** stdlib.h. You might as well include it
    ** anyway so you can use the return status
    ** macros
    */
    #include<stdlib.h>
    /* void main is totally wrong, sorry */
    int main(void)
    {
      FILE *infile;
      /* Allocate memory or use an array, segfaults
      ** are no fun.
      */
      char line[BUFSIZ];
    
      if((infile = fopen("info.txt", "r"))==NULL )
      {
        /* perror is much more suited to error reporting
        ** after standard function calls, you get better
        ** information. And since you're exiting anyway,
        ** why not just return and use a more informative
        ** condition so that readers know the program is
        ** terminating due to error.
        */
        perror("Cannot open input file");
        return EXIT_FAILURE;
      }
    
      /* fgets is much better for line input, either from
      ** a file or from stdin.
      */
      while(fgets(line, sizeof line, infile) != NULL)
        printf("%s\n", line);
    
      /* Always close your files */
      fclose(infile);
    
      /* main returns an int, keep it portable by using
      ** either 0, EXIT_SUCCESS, or EXIT_FAILURE
      */
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    18

    ok

    thank you prelude.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM