Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading a File

Posted on 1998-12-17
6
Medium Priority
?
212 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
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 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 40 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

963 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