Reading information from a file

This is a discussion on Reading information from a file within the C Programming forums, part of the General Programming Boards category; Hi, Just wanted to know if anybody could help me, ive found a test program to see if I could ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    17

    Reading information from a file

    Hi,
    Just wanted to know if anybody could help me, ive found a test program to see if
    I could read information from a text file, and print the information to the screen, prob is when I run the program, visual studio crashes.

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    int main(void)
    {
    	FILE *file_in;
    	int points;
    	char team[20];
    
    	file_in=fopen("Prem.txt", "r");
    
    	while (!feof(file_in))
    	{
    		fscanf(file_in, "%s, %d", team, &points);
    	}
    
    	fclose(file_in);
    
    	printf("Team: %s", team);
    }
    I think it may have a problem finding the text file on my computer

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Check if file_in is equal to NULL before you use it.

    Don't use feof() to control a loop in this manner... http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    fscanf is clearly bad. My signature contains more information.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    thanks for the replies, but I just wanted to know if the
    Code:
    file_in=fopen("Prem.txt", "r");
    is correct,

    just say the file I want to read in is in the C:\ directory, how would I tell the program to look in that directory inorder to find the file?.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Code:
    file_in=fopen("C:\\Prem.txt", "r");
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Hi
    I added the code to my test program, but it doesnt seem to print the value of sum. I was hoping it would read the string in the txt file, which was just "PBC", then make the conversion into numbers, then print the value to the screen.

    But when I build it, it has no errors, but when I try to run it it just prints out

    Answer = <null>


    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    
    {
    
       FILE *file_in;
       char letter[20];
       int counter;
       int sum;
       int length;
    
       if ((file_in = fopen("C:\\Users\\Gopi\\Documents\\roman.txt", "r")) == NULL)
      {
        perror ("C:\\Users\\Gopi\\Documents\\roman.txt");
        
      }
    
       sum = 0;
    
       length = strlen(letter);
       strupr(letter);
       
       
    
       for ( counter = 0; counter < length; counter++ )
     { 
       if ( letter[counter] == 'P' )
       {
    	  sum = sum + 1; 
       }
       else if ( letter[counter] == 'B' )
       {
    	  sum = sum + 5; 
       }
       else if ( letter[counter] == 'C' )
       {
    	  sum = sum + 10; 
       }
    
       
    
      }
    
    while (!feof(file_in))
      {
        fgets(letter, sizeof(letter), file_in);
        printf ("Answer = %s",sum);
        
      }
      
      fclose(file_in);
        
      return(0);
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    You messed up your application structure! You try to process data before reading anything from the file! Re-think your design!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    It amazes me how people will put code together with no logic. Look at what you're doing. You have code which analyzes the length of a string before you even set it to anything. Then you try to analyze the contents of the buffer before you even read from it. Lastly, you try to read from the file....

    To top it all off you incorrectly use feof() as a string, and then when you try to print the answer you format sum, which is an integer, to be printed as a string.

    It's no wonder none of this works. You have to do everything explicitly and logically.

    Do it closer to this order:

    1. Open file.
    2. Read from the file.
    3. Analyze length of buffer and buffer contents.
    4. Print out final result in the right format.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  3. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  4. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM

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