Problem in displaying records in a file

This is a discussion on Problem in displaying records in a file within the C Programming forums, part of the General Programming Boards category; I have written a code to create a file and store 3 records each record has a string and a ...

  1. #1
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52

    Exclamation Problem in displaying records in a file

    I have written a code to create a file and store 3 records each record has a string and a number.

    My code successfully creates the file but i cannot read and display its contents.

    Please tell me where i went wrong?

    Code:
    #include<stdio.h>
    main()
    {
    	FILE *fptr;
    	int n;                            //number declaration
    	char a[10];                  //string declaration
    
    	fptr=fopen("myfile.txt","w");   //opening file
    	if(fptr==NULL) printf("Error opening file!");  //error 
    
    	for(n=1;n<=3;n++)      //loop for writing to file
    	{
    		gets(a);
    		fprintf(fptr,"%s %d\n",a,n);
    	}
    	fclose(fptr);     //closing file
    
    	fptr=fopen("myfile.txt","r");   //opening in reading mode
    	while(fscanf(fptr,"%s %d\n",a,&n)!=EOF)
    		printf("%s %d\n",a,n);   //display
    	fclose(fptr);
    }
    Last edited by logicwonder; 11-30-2005 at 07:00 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Code:
    fptr=fopen(name,"r");
    Undeclared.

    Also, you aren't writing to the file correctly.
    Last edited by SlyMaelstrom; 11-30-2005 at 06:03 AM.
    Sent from my iPadŽ

  3. #3
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    Quote Originally Posted by SlyMaelstrom
    Code:
    fptr=fopen(name,"r");
    Undeclared.

    Also, you aren't writing to the file correctly.
    Sorry that was a type mistake..
    The code has some other problem please help!!
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    I hate to scold you, but you're not really being too polite about this. This forum isn't meant to be a debugger. It's meant to help you along with learning to program. I told you that you had a problem writing to the file correctly, so why don't you start there. The come back to us with an actual attempt at trying to fix your program.
    Sent from my iPadŽ

  5. #5
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    Sorry friend I have made corrections in the original postan I think the problem is in the line
    Code:
    while(fscanf(fptr,"%s %d\n",a,&n)!=EOF)
    Is this not the right method of reading records from the file?
    Last edited by logicwonder; 11-30-2005 at 06:44 AM. Reason: missed one thing

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Nope, that's not the line.

    Here is a tip: Run the program, then open the file in a text editor. Does it look the way you want it to?
    Sent from my iPadŽ

  7. #7
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    yes the file is in proper format.
    Sample file: myfile.txt

    aaaa 1
    bbbb 2
    ccccc 3

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Alright here is the answer.
    Code:
    printf("%s %d\n",a, n) // You need that.
    Again, sorry I didn't see this eariler. I just need some sleep.
    Last edited by SlyMaelstrom; 11-30-2005 at 06:59 AM.
    Sent from my iPadŽ

  9. #9
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    I found my while loop was not working Why? i dont know!

  10. #10
    Registered User Baaaah!'s Avatar
    Join Date
    Oct 2005
    Location
    UK
    Posts
    23
    Quote Originally Posted by logicwonder
    I found my while loop was not working Why? i dont know!
    You could perhaps try this:

    Code:
        while((c = fscanf(fptr,"%s", a)) != EOF)
    Note the additional brackets around the fscanf() function. This will force fscanf to be executed first and pass a value into the variable 'c'. This value will then be compared to EOF.

    I think in you original code the comparison to EOF is made before the function executes, due to the precedence of !=

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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