Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem while splitting the line using a delimiter and pushing itinto an array

Reply
Thread Tools

Problem while splitting the line using a delimiter and pushing itinto an array

 
 
Prasanth
Guest
Posts: n/a
 
      11-21-2008
I have file named as "test.txt". The contents of the file are as
follows :

item1,sal,1000
item2,sal,2000

I am trying to read the file. And spilt each line as per delimiter ","
and push it into an array. That is

array[0][0] = item1
array[0][1] = sal
array[0][2] = 1000
array[1][0] = item2
array[1][1] = sal
array[1][2] = 2000

Please help me out over here

The program which i have written is below:

#include <iostream.h>
#include <string.h>
#include <conio.h>
#include <fstream.h>
#include <process.h>

int main()
{
clrscr();
ifstream inf;
char line[80];
char a[10][10];
inf.open("c:\\test.txt");

{
int j=0;
int w=0;
int i;
while(!inf.eof())
{
cout << line;
for(i=0;i<80;i++,j++)
{
cout << line[i] << endl;
a[w][j] = line[i];
if(line[i] == ' ')
w++;
}
}
}
cout << a[1];
inf.close();
getch();
return 0;
}

Please help me out as soon as possible.

Thanks,
Prasanth
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      11-21-2008
Prasanth wrote:
> I have file named as "test.txt". The contents of the file are as
> follows :
>
> item1,sal,1000
> item2,sal,2000
>
> I am trying to read the file. And spilt each line as per delimiter ","
> and push it into an array. That is
>
> array[0][0] = item1
> array[0][1] = sal
> array[0][2] = 1000
> array[1][0] = item2
> array[1][1] = sal
> array[1][2] = 2000
>
> Please help me out over here
>
> The program which i have written is below:
>
> #include <iostream.h>

Non-standard header.
> #include <string.h>
> #include <conio.h>

Non-standard header.
> #include <fstream.h>

Non-standard header.
> #include <process.h>

Non-standard header.

>
> int main()
> {
> clrscr();
> ifstream inf;
> char line[80];
> char a[10][10];

prefer std::vector to arrays.

> inf.open("c:\\test.txt");
>
> {
> int j=0;
> int w=0;
> int i;
> while(!inf.eof())

This does not do what you think it does. See FAQ 15.5
(http://parashift.com/c++-faq-lite/in....html#faq-15.5)

> {
> cout << line;

Where do you ever get data into line?

> for(i=0;i<80;i++,j++)
> {
> cout << line[i] << endl;
> a[w][j] = line[i];
> if(line[i] == ' ')
> w++;
> }
> }
> }
> cout << a[1];
> inf.close();
> getch();

unneeded.
> return 0;
> }
>
> Please help me out as soon as possible.
>
> Thanks,
> Prasanth

 
Reply With Quote
 
 
 
 
Lionel B
Guest
Posts: n/a
 
      11-21-2008
On Fri, 21 Nov 2008 05:37:50 -0800, Prasanth wrote:

> I have file named as "test.txt". The contents of the file are as follows
> :
>
> item1,sal,1000
> item2,sal,2000
>
> I am trying to read the file. And spilt each line as per delimiter ","
> and push it into an array. That is
>
> array[0][0] = item1
> array[0][1] = sal
> array[0][2] = 1000
> array[1][0] = item2
> array[1][1] = sal
> array[1][2] = 2000
>
> Please help me out over here
>
> The program which i have written is below:



Phew, there are so many things wrong with this I don't know where to begin

Firstly, if you're to program in C++, better to use the C++ (as opposed to
C) headers: they're called things like <iostream>, <fstream>, etc.
(no .h). The difference is that they put all functionality in "namespace
std" (read up about namespaces).

> #include <iostream.h>
> #include <string.h>
> #include <conio.h>
> #include <fstream.h>
> #include <process.h>
>
> int main()
> {
> clrscr();
> ifstream inf;
> char line[80];
> char a[10][10];


Usually a bad idea to use arrays, particularly with arbitrary sizes (and
no error checking in your code). Those are all buffer overflows waiting to
happen.

> inf.open("c:\\test.txt");
>
> {


Why the extra nested block here?

> int j=0;
> int w=0;
> int i;


In C++ it's generally viewed as good style to declare variables just
before you use them.

> while(!inf.eof())


This doesn't do what you think it does... anyway, this is not the best way
to read lines of text (see below)

> {
> cout << line;


Um... you've not actually *read* the line from the file"!

> for(i=0;i<80;i++,j++)


Note that "i" here is not the same "i" you declared above (but j is).

> {
> cout << line[i] << endl;
> a[w][j] = line[i];
> if(line[i] == ' ')
> w++;
> }


Yikes.

> }
> }
> cout << a[1];
> inf.close();
> getch();
> return 0;
> }
>
> Please help me out as soon as possible.


This looks a lot like homework... if it is, the code below should fail
you, as there's no way it could be your own work

Read up in particular about the std::string class and std::vector template
class. They're probably the most useful things you'll ever use in C++.

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

// all standard library stuff is in namespace std
// Don't use this in a header file, though.
using namespace std;

int main()
{
ifstream inf("test.txt");
if (!inf) {
cerr << "Failed to open file\n";
return 1;
}

// read about std::vector
vector< vector<string> > a;

// read about std::string
string line;

// This loop works, because getline() returns false (sort of)
// when there is nothing more to read.

while (getline(inf,line)) {

cout << "line = \"" << line << "\"\n";

a.push_back(vector<string>());

// We use find() to find the delimiters and
// substr() to yank out the text
string::size_type pos1 = 0;
string::size_type pos2 = line.find(',');
while (pos2 != string::npos) {
a.back().push_back(line.substr(pos1,pos2-pos1));
pos1 = pos2+1;
pos2 = line.find(',',pos1);
}
a.back().push_back(line.substr(pos1,pos2-pos1));
}

// Now eof should be set
if (!inf.eof()) {
cerr << "Something went wrong reading file\n";
return 1;
}
inf.close();

for (size_t i=0;i<a.size();++i) {
cout << '\n';
for (size_t j=0;j<a[i].size();++j) {
cout << "a[" << i << "][" << j << "] = \"" << a[i][j] << "\"\n";
}
}

return 0;
}


--
Lionel B
 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      11-21-2008
On Fri, 21 Nov 2008 15:03:31 +0000, Lionel B wrote:

> On Fri, 21 Nov 2008 05:37:50 -0800, Prasanth wrote:
>

....
>> int j=0;
>> int w=0;
>> int i;

....
>> for(i=0;i<80;i++,j++)

>
> Note that "i" here is not the same "i" you declared above (but j is).


Um, scratch that.

--
Lionel B
 
Reply With Quote
 
Prasanth
Guest
Posts: n/a
 
      11-22-2008
Thanks for the advices will try to re-check up the code and come back
to you people in case of the further quires

Thanks,
Prasanth
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading files, splitting on a delimiter and newlines. Bruno Desthuilliers Python 8 07-26-2007 06:45 AM
Splitting a sentence with delimiter preserved Ajithkumar Warrier Ruby 0 10-17-2006 08:25 PM
weird behavior pushing an array onto an array eagle eyes joe Ruby 3 09-28-2004 06:48 PM
Using readline with a new delimiter of line ? Laurent Python 3 12-15-2003 01:47 PM
Splitting and keeping the delimiter Sandman Perl Misc 7 09-12-2003 12:40 PM



Advertisments