Solved

read data from file

Posted on 2002-03-16
3
243 Views
Last Modified: 2010-04-01
Hi experts,

I have problem in reading data correctly from my file file.

The format is as follows:

Company ABC
3  A1
5  B1
6  C1
0
Company XYZ
6  A1
8  B1
5  C1
0
Company RTG
1  A1
4  B1
3  C1
0

My code as follows:

ifstream inFile;
inFile.open("input.txt");

inFile.getline(company, 40);
cout <<company<<endl;
inFile >>cost>>product;

while(!inFile.eof()){
     //How should I detect the '0' char?
        //I need to manipulate the three data
        //(company,cost,product) for the first
        //record before I move to the another
        //record by detecting the '0' char.
}

Pls include explanation with your solution.Thanks!
p.s Any C++ tutorial for beginner like me?
0
Comment
Question by:Celia
[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
3 Comments
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 50 total points
ID: 6870846
Assume the variable cost is declared as an integer (i.e. int cost;).

You need to declare a temporary variable, say temp, of type char (i.e. char temp;).

Here is a sample code of the while loop:

====================================================
  while (!inFile.eof()) {
    inFile >> cost;

    if (inFile.eof()) // detecting the end of file to avoid repeating at the end
      break;

    if (cost==0) { // end of record
      inFile.get(temp); // why? see explanation below.
      inFile.getline(company, 40);
    }
    else {
      inFile >> product;
    }
  }
===================================================
The reason for using a get statement is because we want to skip the "end of line" character after 0. If not using this, then it will falls into infinity loop, since getline does not skip the "end of line" character.
0
 
LVL 12

Expert Comment

by:lexxwern
ID: 6875876
or else you use 0 as the seperating character.
use \n instead. cuse the u can use the << and >> operators with much ease.

Let this be the sample Data
Company ABC
3  A1\n
5  B1\n
6  C1\n
\n
Company XYZ
6  A1\n
8  B1\n
5  C1\n
\n
Company RTG
1  A1\n
4  B1\n
3  C1\n
\n
sample code


struct YOUROBJ company[10];
fstream read("YOURFILE");int c=0;
for(int i=0; i<10;i++)
{
  while(read)
  {
    char temp[1000];
    read>>temp;
    if(!temp) break;
    strcpy(company[i].SUBOBJECT[c++],temp);
  }
}

this is just an example on how to go about using the \n and << >> operators to your advantage. Other solutions are also present.


lhn.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6955389
Dear Celia

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "pkwan"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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