Reading from a file

This is a discussion on Reading from a file within the C Programming forums, part of the General Programming Boards category; Hey folks, Can someone please help me out with this bit of code I have to read a simple text, ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    13

    Reading from a file

    Hey folks,

    Can someone please help me out with this bit of code I have to read a simple text, it just doesnt work and I cant work out why. Been mulling over this for a few hours now looking at examples but cant find a solution.

    Code:
    /* INCLUDE STANDARD INPUT/OUTPUT FUNCTIONS */
    #include <stdio.h>
    #include <string.h>
    
    
    /* OPEN FILE FOR READING INTO CONSOLE */
    int main()
    {
    	FILE *file_ptr;
    
    	/* DELCARE VARIABLES TO STORE SCORE FILE DATA */
    	int user_score, i;
    	char user_handle[25];
    		
    		/*  CREATE A FILE POINTER TO OPEN RELEVANT FILE */
    		file_ptr = fopen("scores.txt", "r");
    
    
    		/* START LOOP TO READ THROUGH FILE & PROCESS FILE TO CONSOLE */
    		do
    		{
    			if ((i=getc(file_ptr)) != EOF) 
    			{
    				fscanf(file_ptr, "%d %s", &user_score, user_handle);
    				printf("\n %d %s", user_score, user_handle);
    			}
    			else break;
    			printf("\n");
    			fclose(file_ptr);
    			return 0;
    		}
    }
    All help is appreciated.

    Thanks.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    this line of code
    Code:
    if ((i=getc(file_ptr)) != EOF)
    will read one character and that one will be missing from user_score.

    guess that should do what you want
    Code:
    /* INCLUDE STANDARD INPUT/OUTPUT FUNCTIONS */
    #include <stdio.h>
    #include <string.h>
    
    
    /* OPEN FILE FOR READING INTO CONSOLE */
    int main() {
        FILE *file_ptr;
    
        /* DELCARE VARIABLES TO STORE SCORE FILE DATA */
        int user_score, i;
        char user_handle[25];
    
        /*  CREATE A FILE POINTER TO OPEN RELEVANT FILE */
        file_ptr = fopen("scores.txt", "r");
    
        /* START LOOP TO READ THROUGH FILE & PROCESS FILE TO CONSOLE */
        while ( 2 ==  fscanf(file_ptr, "%d %s", &user_score, user_handle) ) {
                printf("\n %d %s", user_score, user_handle);
        }
        
        printf("\n");
        fclose(file_ptr);
        return 0;
    }
    This will loop as long as an int and string pair can be read (fsanf returning 2 ).
    Kurt

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >it just doesnt work and I cant work out why.
    "It doesn't work" is a useless description of your problem.

    >/* INCLUDE STANDARD INPUT/OUTPUT FUNCTIONS */
    Why do you feel the need to comment stdio.h, but not stdlib.h? The latter is vastly more confusing in what it does. Then again, anyone with any claim to familiarity with C will know what both are immediately, so a comment is unnecessary.

    >/* OPEN FILE FOR READING INTO CONSOLE */
    Okay, but what does this function do?

    >/* DELCARE VARIABLES TO STORE SCORE FILE DATA */
    Close, but you're still describing the mechanism and not the intention.

    >/* CREATE A FILE POINTER TO OPEN RELEVANT FILE */
    What's wrong with:
    Code:
    /* Open file containing score data */
    >/* START LOOP TO READ THROUGH FILE & PROCESS FILE TO CONSOLE */
    Okay, but what does the loop do?

    I assume "it doesn't work" means you get tons of compiler errors? Your syntax for a do..while loop is incorrect.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    Thanks Zuk, the code you provided has work perfectly for what I required. Just going to read through it to see how it works.

    As for your comments Prelude, a little less negativity would be nice, I comment everything because I am required to.

    Yes I did get a syntax error, something regarding an incorrect closing brace.

    Thank you for taking the time to read the post anyway.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >As for your comments Prelude, a little less negativity would be nice
    It's extremely difficult to tell you that you're wrong without seeming negative.

    >Thank you for taking the time to read the post anyway.
    I hope that you've bothered to get past the negativity and actually try to learn what I was showing you.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    Yes I have.

    Next time I have an issue with C and I post on the forum, I will remember to explain myself, both the code and issue I am having with the code better.

    Thanks all the same.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Thank you for taking the time to read the post anyway.
    >Thanks all the same.
    Save the attitude and just say thanks. At least that way I won't be insulted with your implication that my posts were completely unhelpful.

    By the way, don't get used to being spoonfed.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    For the love of god man .. I was not giving you attitude ... some people are just naturally nice to others. I do apologise for being polite.

    As for being spoon fed, I never asked for someone to just "give me code" or be "spoon fed" as you have put it. Zuk kindly produced some code which has resolved my problem, and I am looking over the code to understand how it works so I dont have a similar issue in the future.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Not going anywhere.....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01: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, 11:12 PM

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