Hi all...I really need someones help and real fast. I am trying to read a textfile with multiple columns that are each separated by a #. eg:

Inventory.txt
Nokia Lumia 610#Nokia#Touch#8GB#8#Tango OS#3899.00#
Blackberry Curve 9380#Blackberry#Both#1GB#8#Blackberry OS 7#3499.00#
Samsung Galaxy S3#Samsung#Touch#32GB#8#Andriod#6999.00#
iPhone 4S#Apple#Touch#16GB#8#IOS#7899.00#

I am trying to save each bit of data in separate variable in a structure because later on I am going to perform calulates and editing but for now. I cant seen to get it to work. When I run my code, it only displays the first line and nothing else. Plus it shows a weird number at the beginning: 1.13002e-317

Can someone help correct my code to display all of the textfile in neat columns and store each in its owm variable correctly.
ps: I cant help think I need to be using Pointers, can someone help with that too

#include<fstream>
#include<iostream>
#include<string>
using namespace std;

struct CellphoneInfo
{
    string name;
    string manu;
    string cellDesign;
    string internalMem;
    string camera;
    string os;
    double costPrice;
};

void outputData(CellphoneInfo [],int);
int main()
{
    string input;
    const int size = 100;

    CellphoneInfo *cellphonePtr;

    fstream dataFile;
    dataFile.open("Inventory.txt",ios::in);
    int count =0;

    if(dataFile)
    {
        while(dataFile)
        {
            count++;
            getline(dataFile,cellphone[count].name,'#');
            getline(dataFile,cellphone[count].manu,'#');
            getline(dataFile,cellphone[count].cellDesign,'#');
            getline(dataFile,cellphone[count].internalMem,'#');
            getline(dataFile,cellphone[count].camera,'#');
            getline(dataFile,cellphone[count].os,'#');
            dataFile >> cellphone[count].costPrice;
        }       

        dataFile.close();
        cout << "File read in" << endl;
        outputData(cellphone,count);

    }
    else
    {
        cout << "Error: Cannot open file.\n";
    }
    return 0;

}

void outputData(CellphoneInfo arr [],int count)
{
    cout << "CellName\tMan Name\tDesign\tMemory\tCamera\tOS\tPrice\n\n"; 
    for(int i = 0;i < count;i++)
    {
        cout << arr[i].name << "\t" << arr[i].manu << "\t" << arr[i].cellDesign << "\t" << arr[i].internalMem << "\t" << arr[i].camera << "\t" << arr[i].os << "\t" << arr[i].costPrice;
    }
    cout << endl;
}

There is no information about cellphone[] in your entire program. One more thing read Click Here

The description for Delim says:

The operation of extracting successive characters is stopped when this character is read.

So if you do getline and pass '#' as delin, it will stop reading data after name.

I can suggest go for getLine but do not use delim, with return string you can always use strtok and get all the information using a loop. To avoid these many statments you can also use two loops:

  1. to read line from text file.
  2. Once line is being read, extract all values using strtok

Hope this will help!nJoy!!

This article has been dead for over six months. Start a new discussion instead.