LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-05-2004, 05:34 PM   #1
tomato
LQ Newbie
 
Registered: Sep 2003
Distribution: Slackware
Posts: 6

Rep: Reputation: 0
fscanf c programming


Hello.

I have just started to program in C.
I wonder why this code doesn't work.
I'm trying to read from a file

The text file looks like this:

Nevermind#Nirvana#1990
Freedom#Neil Young#1980

I'm trying to put the string "Nevermind" in the variable title and the string "Nirvana" in the variable artist and so on.

Code:
void importFile(char file[])
{
        FILE *stream;
        char title[50];
	char artist[50];
        char year[10];
		
	stream = fopen(file, "r");
	
	if(stream != NULL)
	{
	
	
		if(fscanf(stream,"%[^#]", title) != EOF)
			printf("\ntitle: %s", title);
		
		if(fscanf(stream,"%[^#]", artist ) != EOF)
			printf("\nartist: %s", artist);

		if(fscanf(stream,"%[^#]", year ) != EOF)
			printf("\nyear: %s", year);
	
		fclose(stream);
	}
	
}
The outout is:
title: Nevermind
artist: D@
year: D"M@

Thanks
 
Old 01-05-2004, 08:19 PM   #2
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
not a very helpful reply, but it obviously isnt matching anything after the first fscanf.
 
Old 01-05-2004, 08:30 PM   #3
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
also, how do you know if stream is starting from where it left off?
 
Old 01-06-2004, 03:50 AM   #4
codedv
Member
 
Registered: Nov 2003
Location: Slough, UK
Distribution: Debian
Posts: 146

Rep: Reputation: 15
The problem you are having is this.

fscanf(stream,"%[^#]", title) - This statement causes fscanf to read up to but not including the '#' character. That means that when fscanf stops reading characters the file pointer is placed on the '#' character.

In the next statement you do the same:
fscanf(stream,"%[^#]", artist);

However as the file pointer is already positioned on a '#' it doesn't read anything in.

The third statment is incorrect:
fscanf(stream,"%[^#]", year );

As there is no '#' character at the end of year - just a newline.

To cure this problem you need to read these characters off of the buffer. Heres some modified code:
Code:
#include <stdio.h>

void importFile(char file[])
{
        FILE *stream;

        char title[50];
        char artist[50];
        char year[10];

        stream = fopen(file, "r");

        if(stream != NULL)
        {
                /* the extra # will clear the # off the buffer */
                if(fscanf(stream,"%[^#]#", title) != EOF)
                        printf("\ntitle: %s", title);

                
                if(fscanf(stream,"%[^#]#", artist ) != EOF)
                        printf("\nartist: %s", artist);

                /*you can use %s here because fscanf stops reading when it
                  * hits a whitespace character
                  */
                if(fscanf(stream,"%s\n", year ) != EOF)
                        printf("\nyear: %s", year);

                fclose(stream);
        }
}
 
Old 01-07-2004, 09:06 AM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Or you could simply increment stream by 1 on successive reads:

Code:
if(fscanf(stream,"%[^#]", title) != EOF)
	printf("\ntitle: %s", title);
		
if(fscanf(stream+1,"%[^#]", artist ) != EOF)
	printf("\nartist: %s", artist);
And as the previous guy said, there is no # at the end so either search for a \n on the last read or put a # at the end of the year.
 
Old 01-07-2004, 11:12 AM   #6
tomato
LQ Newbie
 
Registered: Sep 2003
Distribution: Slackware
Posts: 6

Original Poster
Rep: Reputation: 0
thanks, it works.
 
Old 01-09-2004, 12:09 PM   #7
codedv
Member
 
Registered: Nov 2003
Location: Slough, UK
Distribution: Debian
Posts: 146

Rep: Reputation: 15
Quote:
Originally posted by jtshaw
Or you could simply increment stream by 1 on successive reads:

Code:
if(fscanf(stream,"%[^#]", title) != EOF)
	printf("\ntitle: %s", title);
		
if(fscanf(stream+1,"%[^#]", artist ) != EOF)
	printf("\nartist: %s", artist);
And as the previous guy said, there is no # at the end so either search for a \n on the last read or put a # at the end of the year.
How does incrementing the file stream work?

If the file stream is a structure then surly incrementing it would be like incrementing an array
 
Old 01-09-2004, 12:11 PM   #8
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
If I am not mistaken, stream is a pointer to the spot in the file you are currently at. Incrementing it would increment the pointer so you point to the next character in the stream.
 
Old 01-09-2004, 12:30 PM   #9
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Instead of blindly incrementing pointers that may get dereferenced later (segfault anyone?), you should just grab the separator character into a throwaway variable. I submit the following (untested) code as explanation of what I mean:

Code:
#include <stdio.h>

void importFile(char file[])
{
        FILE *stream;

        char title[50];
        char artist[50];
        char year[10];
        char separator = '';

        stream = fopen(file, "r");

        if(stream != NULL)
        {
                /* the extra # will clear the # off the buffer */
                if(fscanf(stream,"%[^#]", title) != EOF)
                        printf("\ntitle: %s", title);

                fscanf("#", separator);   /* Maybe check return value ?? */
                
                if(fscanf(stream,"%[^#]", artist ) != EOF)
                        printf("\nartist: %s", artist);

                fscanf("#", separator);   /* Maybe check return value ?? */

                /*you can use %s here because fscanf stops reading when it
                  * hits a whitespace character
                  */
                if(fscanf(stream,"%s\n", year ) != EOF)
                        printf("\nyear: %s", year);

                fclose(stream);
        }
}
You might even abstract out the separator into a #define so that you can easily make it something that's configurable later.
 
Old 01-09-2004, 02:09 PM   #10
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Ya, your right. Incrementing the pointer is a horrible idea. I was thinking to well apparently.
 
Old 01-09-2004, 02:21 PM   #11
codedv
Member
 
Registered: Nov 2003
Location: Slough, UK
Distribution: Debian
Posts: 146

Rep: Reputation: 15
This:
fscanf(stream,"%[^#]#", artist);

Does the same as this:

fscanf("#", separator);
fscanf(stream,"%[^#]", artist);
 
Old 01-09-2004, 06:15 PM   #12
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Can't you just do:

fscanf(stream, "%s#%s#%s", artist, title, year);

?
 
Old 01-09-2004, 07:00 PM   #13
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
i think that will go put whatever matches "%s#%s#%s" into artist?
 
Old 01-09-2004, 08:02 PM   #14
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I don't think so. Give it a shot.
 
Old 01-09-2004, 08:33 PM   #15
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Quote:
Originally posted by codedv
This:
fscanf(stream,"%[^#]#", artist);

Does the same as this:

fscanf("#", separator);
fscanf(stream,"%[^#]", artist);
You're right, the only advantage of separating them out would be evaluating the error conditions separately and I can't see how that would be helpful for this case.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fscanf returns -1 trutnev Programming 2 06-23-2005 08:50 AM
Difference between Top Down programming & Bottom up programming minil Programming 1 06-17-2005 02:42 AM
fscanf & fgetc help billybob2004 Programming 2 02-04-2004 10:24 AM
C fscanf / feof question. Claus Programming 1 02-02-2004 11:55 PM
using fscanf function Linh Programming 2 07-14-2003 12:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration