?
Solved

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

Posted on 2003-03-28
7
Medium Priority
?
216 Views
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)
 thanks;
else
 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 ?

Thanks.
0
Comment
Question by:rmorphis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Accepted Solution

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

pseudocode

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.



0
 
LVL 1

Expert Comment

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

http://www.opengroup.org/onlinepubs/007908799/xsh/fscanf.html
0
 
LVL 1

Expert Comment

by:vamshi_pavan
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.
#include<stdio.h>
#include<conio.h>
void main()
{    FILE *fp;
    int i=0;
    char a,temp[80];
    fp=fopen("filename.txt","r");
    a=getc(fp);
         while(a!=EOF || a!=';')
         {    temp[i]=a;
              printf("%c",a);
              a=getc(fp);
              i++;
          }
          temp[i]='\0'
         fclose(fp);
}
0
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!

 
LVL 1

Author Comment

by:rmorphis
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.

0
 
LVL 22

Expert Comment

by:grg99
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.

0
 
LVL 8

Expert Comment

by:adg080898
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);
}
0
 
LVL 1

Author Comment

by:rmorphis
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.

R.
0

Featured Post

Technology Partners: 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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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