Solved

Reading a File

Posted on 1998-12-17
6
190 Views
Last Modified: 2010-04-15
I have files in the following format:

Basket 1:
    Field1=xxxx
    Field2=xxx
    Field3=xxx
Basket 2:
    Field1=xxxx
    Field2=xxx
    Field3=xxx
Basket 3:
    Field1=xxxx
    Field2=xxx
    Field3=xxx
Basket 4:
    Field1=xxxx
    Field2=xxx
    Field3=xxx
...........and so on

No of baskets varies in each file.But the no. of fields are same in each basket.

My aim is:
Based on the Field1 value in each basket I want perform some
operations on Field2 and Field3 values and write the output to some other file.

Can somebody help, how to start.any sample code will be more helpful.
0
Comment
Question by:anilkumarv
6 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1255268
The basic approach is

(a) Read the file into a buffer using fopen, fread, fclose
(b) parse the buffer into an internal data structure (say an array in C, or map/multimap in C++ - easier with C++).  Use strchr or similar to break on new lines, colons, equals or other significant characters.
(c) Modify internal data structure
(d) Write out internal data structure using fopen, fwrite, fclose

Which part of the above do you need help on - or all of em ?
0
 

Author Comment

by:anilkumarv
ID: 1255269
Mainly i need help in parsing.

thanx
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255270
use strtok to split up input lines
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Expert Comment

by:laeuchli
ID: 1255271
I would put it in an ini file and use the windows ini file reading functions to read it. They are very easy to use.


0
 

Author Comment

by:anilkumarv
ID: 1255272
I am on unix not on windows.


0
 
LVL 11

Accepted Solution

by:
alexo earned 20 total points
ID: 1255273
#include <stdio.h>

#define FORMAT "Basket %d: Field1=%d Field2=%d Field3=%d\n"
#define FIELDS 4

void main()
{
    FILE *f;
    int n, a, b, c;

    f = fopen("d:/temp/qq.txt", "r");
    while (fscanf(f, FORMAT, &n, &a, &b, &c) == FIELDS)
    {
        printf("%d: %d %d %d\n", n, a, b, c);
    }

    fclose(f);
}
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now