PB reading text file with getline(fichier, ligne,' ')

hello,
i want to read a text file using Vc++. so i develop this code with some help from internet.
the data in the text file is like that:
74.60038368625241 96.79636560473779 112.45476845156641 ....
96.81124655492178 125.60459081423575 145.88186076475341 ..

all the numbers are seperated by space.

i want to read each number and put them into the matrix named xoff.
the first line is read successfuly but in the second line it begin with the second number 125.60459081423575 not with 96.81124655492178. in the third line it begin with the third element etc...

could some one help me to solve this PB.

thanks an advance.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
 
using namespace std;
 
int main()
{

	FILE * output_file2=fopen ("D:/main/test1.txt" , "w" );
	///

	int i=0,w=320,H=280,j;
	int size=w*H ;
	double *xoff =new double[w*H];
	double x=0;
        ifstream fichier("test2.txt", ios::in);  // open the file
 
        if(fichier)
{
        string ligne;
        while(getline(fichier, ligne,' '))  
        {		
			x=atof(ligne.c_str());//conversion from string-->double
			xoff[i]=x;
			
            //   cout <<x << endl;  // on l'affiche
		//	printf("xoff[%d]=%lf\n",i,xoff[i]);
			i++;
        }
}

        else
                cerr << "Impossible d'ouvrir le fichier !" << endl;


		
	int dim_allouee=0;
	double **gr=0; 

    gr = new double*[H]; 

    for( dim_allouee=0; dim_allouee<H; dim_allouee++)
		{ 
			gr[ dim_allouee] = new double[w]; 
			
        } 

		for(i=0;i<H;i++)
		for(j=0;j<w;j++)		
			{
				//printf("xoff[%d]=%lf\n",j,xoff[j]);
				gr[i][j]=xoff[w*i+j];
				
			}

		for(i=0;i<H;i++)
		   {
			   for(j=0;j<w;j++)
					{
					fprintf(output_file2,"%.14lf ",gr[i][j]);
			  
					}
				 fprintf(output_file2,"\n");
		  }	 
        return 0;
		 delete[]xoff;
		 for(i=0;i< dim_allouee;i++) 
		{ 
			delete[] gr[i]; 
		
		} 
	delete [] gr;

}

Last edited on
Please edit your post and put the source inside code tags. It will make your post a lot more legible and folks here will be more likely to look at it.
Topic archived. No new replies allowed.