Arrg!! Simple fileIO question, easy 200 PTS! HELP!!

Posted on 2003-03-28
Medium Priority
Last Modified: 2010-04-15
Don't ask why but I'm stuck in a C class and I can't use C++ for what I need to do. Before you read on, If your any geek who's going to waste my time with "no homework" comments, don't. It's a simple syntax problem and I wouldn't be posting if I didn't already waste 2 hours on this. Your getting paid in points so...

if( you can help)
 move on;

I'm reading from a file and I need to detect a delimiter that separates two values on a line, I've been trying to use
fscanf, but can't find any clear documetation on setting it to stop at a delimiter.

Here's an example of the line being read:

Doe, John; GPA=3.4

I need to read "Doe, John" into a *string, and 3.4 into another.

The list is going to have names of varying length so I can't use a set value length to read.

I need to read the line up until the semi colon. How?

After that I'm going to use GPA=%f to read the grade value. I think I got that right?

I need to pull about 50 names in a linked list and sort it all alphabetically, then do a bunch of various deletes, insertions and outputs,

but I'm stuck from the get go on this stupid syntax issue.

could someone explain:  

fscanf(*filename," \"%[^\"];, %*c);

and show me how to write this line to do what I need ?

Question by:rmorphis

Accepted Solution

frogger1999 earned 800 total points
ID: 8229348
Assuming that your line is exactly correct you basically need to do this


scan stream till you find ';' store in string then scan this exact string "; GPA=" then scan a float and store it in a float.  

end pseudo

in the docs it says to scan a string not delimited by whitespace you need to use the '[]' characters filled with a representation of what you need.  If the first character is '^' then it means it is reversed (find all except this character)

as stated in our first step we want to scan until we hit the ';' character so that means [^;] right;

format string so far is "%[^;]"

then for our second step...

we need to get rid of some text basically so we can get to our float right.  The docs say we can just put this text in explicitly to scan it but not store it so we just add "; GPA=" to our format string

format string so far is "%[^;]; GPA="

finally we need to read in a float number to end the line.  This is real easy.  According to spec to read a float in we just have the specifier %f so we add that to our string

our final format string

format string so far is "%[^;]; GPA=%f"

so now you are set you can just call fscanf like this

fscanf(stream, "%[^;]; GPA=%f", nameBuffer, gpaHolder);

I hope that I explained it well enough, my attempt here was to try to teach you a little about fscanf not just give you an answer.  If anything is unclear please reply.


Expert Comment

ID: 8229355
oh and by the way.  I found this to be a great little resouce on fscanf (besides microsoft docs)


Expert Comment

ID: 8229514
why don't you try using getc(),and check every character if its a delimiter or not and then store those char's you read in some array and then at the end copy them to a string.

This is the right programme but hope it helps you  get the idea.
void main()
{    FILE *fp;
    int i=0;
    char a,temp[80];
         while(a!=EOF || a!=';')
         {    temp[i]=a;
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

ID: 8230254
Thanks frogger, I have 4 books and none of them explain how the delimiter part works. Let me give it a shot and I'll either post a question or award your points.

yamshi, thans for the code, but the fscanf explaination was what I was looking for.

LVL 22

Expert Comment

ID: 8230425
Just FYI, in case yo havent figured this out yourself:

IN general, scanf and cousins, and in general any routine that does a lot of things for you:  is very nice if it does something close to what you want, but often ends up being more trouble than it's worth if you have to tack on tons of extra baggage to get it to do your thing.


Expert Comment

ID: 8230463
So the delimiters are comma, semicolon, equal, and carriage return. Do this:

// Warning pBuf is modified
void trythis(char *pBuf)
    char *pLname, *pFname, *pGPA, *pGPAValue;
    double nGPA;

    pLname    = strtok(pBuf, ",");
    pFname    = strtok(NULL, ";");
    pGPA      = strtok(NULL, "=");
    pGPAValue = strtok(NULL, "\n");

    nGPA = strtod(pGPAValue);

    printf("    Lname: %s\n", pLname);
    printf("    Fname: %s\n", pLname);
    printf("scoretype: %s\n", pGPA);
    printf("scoretext: %s\n", pGPAValue);
    printf(" scoreval: %lf\n", nGPA);

Author Comment

ID: 8231911
That sysntax works, and thanks for the explaination. I'm wondering if fscanf is the way to go for this problem, I seeing some problems when I read in the subsequent lines. I kepp hacking at it, thanks for the help.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question