473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

fgets/sscanf

Tango Charlie
I'm trying to get this program to open and read a *.txt file and save in an array in traditional C. CLASS is defined as "filename.txt" in the header file. It reads from the file, I think, but it is nothing but garbage. Please offer suggestions...

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include "cl_info.h"
  3.  
  4. void get_class_data(struct student class[])
  5. {
  6.    char   last_name[MAXWORD];
  7.    char   line[MAXLINE];
  8.    int    cnt;
  9.    int    i = 0;
  10.    int    n;
  11.    FILE   *ifp;
  12.  
  13.    ifp = fopen(CLASS, "r");
  14.    if (ifp == NULL) {
  15.       printf("\nCannot open %s - Goodbye!\n\n", CLASS);
  16.       exit(1);
  17.    }
  18.    for (i = 0; fgets(line, MAXLINE, ifp) != NULL; ++i){
  19.        cnt = sscanf_s(line, "%s%d %c",
  20.            last_name, &class[i].student_id, &class[i].grade);
  21.        assert(cnt == 3);
  22.        n = strlen(last_name);
  23.        class[i].last_name = calloc(n + 1, sizeof(char));
  24.        assert(class[i].last_name != NULL);
  25.        strcpy(class[i].last_name, last_name);
  26.    }
  27.    if (i != CLASS_SIZE) {
  28.       printf("\n%s\n\n",
  29.          "ERROR: Unexpected class size in %s - Goodbye!\n\n", CLASS);
  30.       exit(1);
  31.    }
  32. }
  33.  
  34.  
I can't wait to understand this language!
Mar 30 '07 #1
1 3802
RedSon
5,000 Expert 4TB
Questions about reading and parsing text files in C has been asked and answered hundreds of times. What resources have you been able to find on this web site and by searching google, and google groups?
Mar 30 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Eirik | last post by:
Hey, all groovy C programmers, I've read in the FAQ(question 12.18) about how applications skip calls to (f)gets() after scanf() has been used. How can I avoid this? I know that I can by putting...
3
by: Tcc | last post by:
Hi all, Assume there are some data in a file "a.txt": abc def 11<---------------------data in a.txt is it possible for me to use "fgets" function to get the string "abc", "def" and "11"...
6
by: AMT2K5 | last post by:
Hello. I have a file (for a school assignment) with the following format and delimiter format. Each record in the file has the following format: 123423454567987,29873,James,Harry,St....
7
by: Michael R. Copeland | last post by:
I'm processing a control file comprised of many types of lines, with some containing variable data. I have a problem parsing the following data: 18 12.2 7.145 6.214 Phase distances First,...
7
by: RSoIsCaIrLiIoA | last post by:
until a poor newbie can build a better function than sscanf and fgets scanf("%s", string) is like gets(string)
1
by: anonymous | last post by:
I'm having a LOT of trouble figuring out how to read input from a file into my program. This file contains lines with an arbitrary number of words in them. For example: This is one line in my file...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
2
by: jou00jou | last post by:
Hi, I have trouble using sscanf and fgets to check for overflow. I will post the assignment specification so I could help whoever would kindly like to offer his/her help. ...
6
by: DrSchwartz | last post by:
Hi all, I'm completely stuck.. I have a program which reads integers (there are 65536 integers) from one file, and adds them into the Bloom filter. And then it reads another file (with a larger set...
6
by: cclark18 | last post by:
Hi everybody I am a C newbie. A simplified version of my problem is below. The issue is that when the code is run, fgets is executed before any of the other code is run. I'm sure its some minor...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.