Hello, I am coping a problem with getline.
I have a txt file that looks like the following.And my task is to read it and put them in a file that contains 10 columns and not 8.

0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,1
0,0,0,0,0,0,1,0
0,0,0,0,1,0,0,0
0,0,0,1,0,0,0,1
0,2,2,1,0,2,6,5
12,17,12,28,39,40,46,71

and I have to read them and put them in an other file
so my code so far is:

int array[32768];
int i=0, j;
char cNum[1000] ;
char dum [256];

if (dum[0]=='0')
		{
			do {
			 myfile.getline(cNum, 256,',');
                                array[i]= (int)atoi(cNum) ;
                                 cout << cNum;
                                i++ ;
			   }
			 while (! myfile.eof() );	
                }
		
	
		else
		{
			continue;
		}
			
		for (j=0;j<i;j++)
		{
			cout << array[j]<<" ";
			cout << cNum << " ";
			if (!(j%10))
				cout <<endl;
		}

And the output file it that one:

0 
0 0 0 0 0 0 1 0 0 0 
0 0 1 0 0 0 0 1 0 0 
0 0 0 1 0 0 0 1 2 2 
1 0 2 6 5 17 12 28 39 40

Can you pls tell me what I am missing here ?? :-(

Recommended Answers

All 5 Replies

Several things come to mind: So I will start at the end of your code.

First is that (!(j % 10 )) . Is this true if j = 0.
Obviously that causes one of your output errors.

Now consider the getline function. You have told it that the linebreak character is
comma. However, when you read past the end of a line in your file, you get a concatenation of the two parts of the line e.g. you get 5\n12 .
You will see this if you add a std::cout<<"cNum =="<<cNum<<"== "<<array[i]<<std::endl; at the line your process cNum.

The if (dum[0]=='0') is obviously from a larger part of the program.

Finally: please read the posts about code tags, and please use them.
Please post slightly more complete code segements, e.g with the definitions etc.

ok, I'll make my question simpler coz I am not a c++ expert and i am copying problem to make someone understand me :-S

the problem i am having is that the numbers have comma between them but the last number and the first number of each sequence are not comma separated so that;s the issue.
i need to use getline command with two delimiters, one "," and the other newline '\n'
how can I do that with my data?

coz the code i use disappears from the output the first number of the sequence

thanks in advance

Since the format of the input is known in advance and it is a very structured input you could use a loop calling getline() nine times with comma as the delimiter followed by a single call to getline() using the new line char as the delimiting char to read each line of input, or you could read the entire line all at one using the newline char as the delimiting first, convert the string to a stringstream, and then parse the stringstream using getline() with the comma as the delimiting char as often as needed.

Lerner is correct if you can use the string.h library you can do that but a way just using thing iostream library.

#include <iostream>
#include <fstream>
using namespace std;
int main()
{

int array[32768];
int i=0, j;
char cNum[1000] ;
char dum [256] = "0";

ifstream myfile ("myfile.txt");
if(myfile)
	if (dum[0]=='0')
		{
			do 
			{
				if(!((i+1)%8))
				{
					myfile.getline(cNum,256);
				}
				else
				{
					myfile.getline(cNum, 256,',');
				}
				array[i]= (int)atoi(cNum) ;
				cout << array[i];
				i++;
			}
			 while (! myfile.eof() );	
        }    
		//Removed statement N/A no loop
		/*
		else
		{
		continue;
		}
		*/
		cout << endl << endl;	
		for (j=0;j<i;j++)
		{
			cout << array[j]<<" ";
			if(!((j + 1)%10))
			{
			cout << endl;
			}
		}
	}

Please in future post provide a compilable example to make it easier to solve your issue.

Thank you very much for your help.It worked perfectly :-)

And I'll pay attention to your notifications concerning a future post.

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.