Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading a File

Posted on 1998-12-17
6
Medium Priority
?
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

722 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