Sponsored Content
Top Forums Programming Correct way to read data of different formats into same struct Post 302940628 by yifangt on Wednesday 8th of April 2015 12:35:26 AM
Old 04-08-2015
Thanks Don!
Yes, I tried your code of post #2, which worked of course. Before my post I tried this version for format2.dat as I thought C takes input by line:
Code:
fscanf (INFILE, "%s", record[n].name);
fscanf (INFILE, "%lf", &record[n].test);
fscanf (INFILE, "%lf", &record[n].quiz);
fscanf (INFILE, "%lf", &record[n].English);
printf ("%s\t%.2lf\t%.2lf\t%.2lf\n", record[n].name, record[n].test, record[n].quiz, record[n].English);
++n;

and this code:
Code:
fscanf (INFILE, "%s %lf", record[n].name, &record[n].test);
fscanf (INFILE, "%lf", &record[n].quiz);
fscanf (INFILE, "%lf", &record[n].English);
printf ("%s\t%.2lf\t%.2lf\t%.2lf\n", record[n].name, record[n].test, record[n].quiz, record[n].English);
++n;

both of which worked, but look very weird even to myself! Along with your version, I have three ways to do the same thing! These give me confusion that brought my post.
Book and tutorials I read use format1.dat as example to fill struct. From the manpage, it is not very clear to me without an exact example:
Code:
A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)).  This directive matches any amount of white  space, including none, in the input.

Handling stream (including parsing) in C is quite challenging to me, it is better now with fscanf(). Thanks a lot again!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read from data file

Hi, I have a data file formatted like this: Ex: Mike 3434 Jack 481 Peter 12 Alan 926 I want to get this data into 2 variables: "Names" and "Numbers" that I can using one "for" loop to get the value as Names and Numbers Like this: for i in 0 1 2 3 do echo $Names echo... (12 Replies)
Discussion started by: fongthai
12 Replies

2. UNIX for Advanced & Expert Users

how to read the data from an excel sheet and use those data as variable in the unix c

I have 3 columns in an excel sheet. c1 c2 c3 EIP_ACCOUNT SMALL_TS_01 select A.* from acc; All the above 3 col shoud be passed a variable in the unix code. 1.How to read an excel file 2.How to pass these data as variable to the unic script (1 Reply)
Discussion started by: Anne Grace
1 Replies

3. Programming

writing binary/struct data to file

I am trying to write binary data to a file. My program below: #include <stdlib.h> #include <stdio.h> struct tinner { int j; int k; }; struct touter { int i; struct tinner *inner; }; int main() { struct touter data; data.i = 10; struct tinner... (4 Replies)
Discussion started by: radiatejava
4 Replies

4. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

5. Shell Programming and Scripting

Extracting data from a log file with date formats

Hello, I have a log file for the year, which contains lines starting with the data in the format of YYYY-MM-DD. I need to get all the lines that contain the DD being 04, how would I do this? I tried using grep "*-*04" but it didn't work. Any quick one liners I should know about? Thank you. (2 Replies)
Discussion started by: cpickering
2 Replies

6. HP-UX

struct utsname throwing error : Value too large to be stored in data type

Hi, I am trying to fetch sysname and nodename using struct utsname. I have two HP-UX servers on with 10 characters and other with 13 characters host name. For the first one I am getting truncated 8 characters as output but for the second one i am getting "Value too large to be stored in data type"... (1 Reply)
Discussion started by: shivarajbm
1 Replies

7. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

8. Shell Programming and Scripting

Help to get correct data using awk

I have this input.|user1 |10.10.10.10 |23|046|1726 (212) |0 |user2 |10.10.10.11 |23|046|43 (17) |0 |test |10.10.10.12 |23|046|45 (10) |0 |test1 |10.10.10.13 |23|046|89 (32) |0 I need to get the data for a user like thisuser1 1726 user2 43 test 45 test1 89... (11 Replies)
Discussion started by: Jotne
11 Replies

9. Shell Programming and Scripting

Shell script to correct the data

Hi, I have below data in my flat file.I would like to remove the quotes and comma necessary from the data.Below is the details I would like to have in my output. Could anybody help me providing the Unix shell script for this. Input : ABC,ABC,10/15/2012,"47,936,164.567 ","1,036,997.453... (2 Replies)
Discussion started by: sonu_pal
2 Replies

10. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies
FGETS(3P)						     POSIX Programmer's Manual							 FGETS(3P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
fgets -- get a string from a stream SYNOPSIS
#include <stdio.h> char *fgets(char *restrict s, int n, FILE *restrict stream); DESCRIPTION
The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1-2008 defers to the ISO C standard. The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a <newline> is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte. The fgets() function may mark the last data access timestamp of the file associated with stream for update. The last data access timestamp shall be marked for update by the first successful execution of fgetc(), fgets(), fread(), fscanf(), getc(), getchar(), getdelim(), get- line(), gets(), or scanf() using stream that returns data not supplied by a prior call to ungetc(). RETURN VALUE
Upon successful completion, fgets() shall return s. If the stream is at end-of-file, the end-of-file indicator for the stream shall be set and fgets() shall return a null pointer. If a read error occurs, the error indicator for the stream shall be set, fgets() shall return a null pointer, and shall set errno to indicate the error. ERRORS
Refer to fgetc(). The following sections are informative. EXAMPLES
Reading Input The following example uses fgets() to read lines of input. It assumes that the file it is reading is a text file and that lines in this text file are no longer than 16384 (or {LINE_MAX} if it is less than 16384 on the implementation where it is running) bytes long. (Note that the standard utilities have no line length limit if sysconf(_SC_LINE_MAX) returns -1 without setting errno. This example assumes that sysconf(_SC_LINE_MAX) will not fail.) #include <limits.h> #include <stdio.h> #include <unistd.h> #define MYLIMIT 16384 char *line; int line_max; if (LINE_MAX >= MYLIMIT) { // Use maximum line size of MYLIMIT. If LINE_MAX is // bigger than our limit, sysconf() can't report a // smaller limit. line_max = MYLIMIT; } else { long limit = sysconf(_SC_LINE_MAX); line_max = (limit < 0 || limit > MYLIMIT) ? MYLIMIT : (int)limit; } // line_max + 1 leaves room for the null byte added by fgets(). line = malloc(line_max + 1); if (line == NULL) { // out of space ... return error; } while (fgets(line, line_max + 1, fp) != NULL) { // Verify that a full line has been read ... // If not, report an error or prepare to treat the // next time through the loop as a read of a // continuation of the current line. ... // Process line ... ... } free(line); ... APPLICATION USAGE
None. RATIONALE
None. FUTURE DIRECTIONS
None. SEE ALSO
Section 2.5, Standard I/O Streams, fgetc(), fopen(), fread(), fscanf(), getc(), getchar(), getdelim(), gets(), ungetc() The Base Definitions volume of POSIX.1-2008, <stdio.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Stan- dard is the referee document. The original Standard can be obtained online at http://www.unix.org/online.html . Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . IEEE
/The Open Group 2013 FGETS(3P)
All times are GMT -4. The time now is 12:47 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy