Hello everyone I'm having a bit of trouble with C. Its been awhile and I was wondering if I'm on the right track I'm trying to read from a file and manipulate the data with a structure I created. The problem is the first two values in the text document will be used for the loop count and the next will be used to subtract from other values in the document. For instance the first value is 10 so go through 10 lines of the document and the next line is the date 20120803. I want to read these two and then for the remainder of the data (student ID, course number, submission date) I want to store those in different variables. I was thinking I would either read the document with fgets and then store the values using sscanf or just store and read through the document using fscanf.

Ahh, we're playing Jeopardy. OK.

The question is: How can I read the file and store student information?

Did I win?

-------------------

In answer to your non-question, looks like either idea would work...

Comments
WaltP is WaltP! He can never change! :-D hahahaha... awesome :-D yes sir, ! you win

The below is my idea(rough one) to proceed to design first towards implementation.

Create structure for first two data

...........
struct StudentIndex
{
 int No_Of_students;
 int date;
   or
 (int year;
 int month;
 int day;) 
};
..............

Fill this structure by read (using fscanf())
first 2-character for no_of_Students as int
next string(8 character) for date (or read as 4-character for year, 2-char for month, 2-char for day)

fscanf(fp, "%d %s ", structInfo.No_Of_Students,structInfo.date);//Or separte it by year, month, day

Then create struture for studentInfo

............
struct studentInfo
{
 int student ID;
 int course_number;
 char *submission_date; or (sepate date for month, day, year);
};
.............

Fill this structure after filled StudentIndex structure

fscanf(fp, "%d %d %s "", structInfo.Student_ID,structInfo.course_number, structInfo.submission_date);

Once these information is filled you can easily read each data.

Edited 4 Years Ago by Perry31

lol sorry should have stated what was wrong. I can read from the file just fine but for the first to values arent apart of the student information.

10
20120803
CS460 e788i902 20120503 100
CS460 r673q456 20120203 50
...

I wanted to store the first two seperately then store the students info in other variables.

I can't tell if you simply made an explanation of what you are going to do or there's some unasked question you need help with.

Please be clear when you post. Both of the posts in this thread are ambiguous at best.

freopen() can be used to read the file and take all the input from there. thanks.

Here is a snippet of the code

#include <stdio.h>

int main(){

FILE *fp;

fp = fopen("test.txt", "r");

char line[500];
char duedate[9];
char course[6];
char ID[9];
char date[9];
int grade;
int num;

while(fgets(line, 500, fp) != NULL)
{
    if(line != "CS460"){
        fscanf(fp,"%d,%s", &num, duedate);
    }
    if(line == "CS460"){
        fscanf(fp, "%s,%s,%s,%d", course, ID, date, &grade);

    }

}
    printf("%d\n%s", num, duedate);
   printf("%s %s %s %d", course, ID, date, grade);
}

Another ambiguous post... Maybe this will help:

[boilerplate_help_info]
Posting requests for help must be well thought out if you want help quickly and correctly. Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful? Check your post with these checkpoints - what is it you missed:

  1. Ask a question that can be answered. Do not ask
    -What's wrong with my code?
    -Why doesn't this work?
    -Anything else that does not give us useful information.
  2. Post your code. If we don't know what you did, how can we possibly help?
    -Use PROPER FORMATTING -- see this
    -Use CODE Tags so your formatting is preserved.
    If we can't follow your code, it's difficult to help. We don't care that you're still working on it. If you want us to read it, it must be readable.
  3. Explain what the code is supposed to do. If we don't know where the target is, how can we help you hit it?
  4. Explain what actually happened! If we don't know where the arrow went when you shot it, how can we tell what went wrong and how far from the target you are?
  5. If you have errors, post them! We can't see your screen. We can't read your mind. You need to tell us what happened.
  6. Do not ask for code. We are not a coding service. We will help you fix your code.
    -If anyone posts working code for you, they are a cheater.
    -If you use that code you are a cheater.
  7. Do not bore us with how new you are. We can tell by your code.
    -Do not apologize. We were all new, and unless you are completely brain dead you will get better.
    -Do not ask us to "take it easy on you."
    -Do not say "I don't know what's going on." That's obvious since you posted for help. Use that time wisely by explaining as best you can so we can help.
  8. Do not apologize for posting 'late'. We don't have any expectations on when you should be posting - 10 minutes or 10 days. We aren't timing your responses.
  9. Do not post your requirements and nothing else. We view that as a lazy do-nothing student that wants us to do their work for them. That's cheating and we will be hard on you.
  10. Do not attach files except when absolutely necessary. Most of us are not going to download files. Add the information to your post.
  11. Do not tell us how urgent it is. Seriously, for us there is no urgency at all. Many that can help will ignore any URGENT or ASAP requests.
  12. Create a good title for your post. The title C++ in the C++ forum is bloody redundant and worthless! What's wrong? equally so. Specifically what are you having trouble with? There is your title. (note: my program is not the answer.)

Think more about your next post so we don't have to play 20 questions to get the info we need to help you.
[/boilerplate_help_info]

This article has been dead for over six months. Start a new discussion instead.