Manipluating with a file!

Hi all,

I have one problem with reading a textfile. My first solution was:
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
 
#include <iostream>
#include <string> 
#include <fstream>

using namespace std;
  
struct Student 
   {
      int no;
      int id;
      string city;
	  float points;
	  int grade;
      Student *next; 
   };
int main()
{
    int seq_num, Id, gr;
    float pn;
    string ct;

    ifstream infile;
    infile.open("d:\\dataset.txt");
   
	Student *head=NULL; 
   
	while (infile)
    {
        infile>>seq_num>>Id>>ct>>pn>>gr; 
										      
      
        Student *newStudent; 
	    newStudent = new Student; 
        newStudent->no = seq_num; 
        newStudent->id = Id;
        newStudent->city = ct;
        newStudent->points = pn;
        newStudent->grade = gr;
        newStudent->next = NULL;
       
        if (head ==NULL) 
            head = newStudent; 
        else
        {
            newStudent->next = head; 
            head = newStudent; 
        }                   
    } 
		
	cout<<"\nNo\tID\tCity\tPoints\tGrades ";
	cout<<"\n\n";

	Student *Display_ptr; 
	Display_ptr = head; 

	while (Display_ptr)
	{
		  cout<<Display_ptr->no<<"\t"
			  <<Display_ptr->id <<"\t"
			  <<Display_ptr->city<<"\t"
			  <<Display_ptr->points<<"\t"
			  <<Display_ptr->grade<<"\t"
			  <<endl;
		Display_ptr = Display_ptr->next; 

	}

	infile.close();
return 0;

}

This code is good and it works. As you can see I need to store each column in different type which is appropriate for it. But now my problem is taht I have another text file, and like this I need to store it but the problem is that in this text file each column is separated with tab, but the length of third column is larger than others. I try to read it with getline() and giving a delimiter but I did'nt succed. I want from to tell me how to use the getline() example:
1
2
3
4
while(textfile_name)
{
cin.getline(column, textfile_name, "\t");
}


I will give you one example of one record from textfile as follows:

15366 4/15/2003 1 HP JORNADA 560 SERIES POCKET PC COLOR 262 2


But with this code I get an errors. Can someone give a suggestions??

Thank you very much.

Last edited on
Actually...the code is very bad...you have a HUGE memory leak, because you are not freeing any of the memory you are allocating with new.

Anyway, what exactly was the error you got?
Topic archived. No new replies allowed.