3 Replies - 5147 Views - Last Post: 16 March 2011 - 05:52 AM Rate Topic: -----

#1 crimsonx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-March 11

I want to read textfile with two 3 lines of number delimited by comma

Posted 16 March 2011 - 12:33 AM

Please help... i want to read text file with 4 lines of integer number and display the result, the first line contain single number, the second and third lines contains 4 numbers delimited by comma and the 4th line is also a single number but my program did not display the contents of the text file. please help me. thanks. below is my text file and c++ source code.

data.txt
2
200, 45, 35, 90
300, 8, 70, 60
5

#include<fstream>
#include<iostream>
#include<cstdlib>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
    ifstream fileData("data.txt");
    string line;
    
     vector <int> intarray;
    while (getline(fileData, line))
    {
           line = line + ','; 
           int npos =0;
           int lpos = 0;
           while ((npos = (int)line.find('*', lpos)) != string::npos)
           {
                if (npos > lpos)  
                {
                     istringstream ss(line.substr(lpos, npos - lpos));
                     int i;
                     if (ss >> i)
                     
                     cout<<i<<" ";
                     intarray.push_back(i);
                }
                lpos = npos+1; 
           }
    }
    
system("pause");
return 0;
}




 

This post has been edited by JackOfAllTrades: 16 March 2011 - 03:47 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: I want to read textfile with two 3 lines of number delimited by comma

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: I want to read textfile with two 3 lines of number delimited by comma

Posted 16 March 2011 - 02:38 AM

Welcome to DIC!

Please edit your posting (see the "EDIT" button in lower right). (If Edit is not available make a new posting on this thread and skip ( a ) below.)
( a ) Delete all your code.
( b ) Get a fresh copy of the code with formatting in place from your editor / IDE.
( c ) Paste the formatted code between code tags like this :code:
Read here for more help with the code tags
http://www.dreaminco...-code-tags-are/
( d ) Use the "Preview Post" button to check it's all good.
( e ) Use the "Submit Modified Post" button to finish the editing.

----

Please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
Copy and paste the errors exactly as they are.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect?
Give details and, ideally, examples.
If you provided inputs to the program tell us what they were.
( e ) What have you already tried to fix it?

----

Don't use system("pause")
Read this to understand why:
http://www.gidnetwork.com/b-61.html

------

You suggested that your program is not producing the expected output.
Please explain which lines of your program you believe will produce output of the data you read from the file.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: I want to read textfile with two 3 lines of number delimited by comma

Posted 16 March 2011 - 03:48 AM

Merged duplicate topics. Please do not create a new topic when you already have one on the same subject.
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: I want to read textfile with two 3 lines of number delimited by comma

Posted 16 March 2011 - 05:52 AM

You were going right except for the fact that you had (int)line.find('*', lpos)

Here's the correct code...
#include<fstream>
#include<iostream>
#include<cstdlib>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
    fstream fileData("m2.txt", ios::in);
    if(!fileData.is_open()) return 0;
    string line;

    vector <int> intarray;
    while (fileData >> line)
    {
           line = line + ',';
           int npos =0;
           int lpos = 0;
           while ((npos = (int)line.find(',', lpos)) != string::npos)
           {
                if (npos > lpos)
                {
                     istringstream ss(line.substr(lpos, npos - lpos));
                     int i;
                     if (ss >> i)

                     cout<<i<<" ";
                     intarray.push_back(i);
                }
                lpos = npos+1;
           }
    }
    fileData.close();



cin.get();
return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1