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
Solved

Reading a File

Posted on 1998-12-17
6
194 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

838 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