ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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);
}
}
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.