Solved

Reading a File

Posted on 1998-12-17
6
191 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

911 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

22 Experts available now in Live!

Get 1:1 Help Now