File Managment, selecting certain parts from a file to write or edit

Im trying to write a program to read a file and determine a secondary index, which will be written to a file, and create lableID file which will hold a label of the data

Idea is kind of clear in my mind so I started simple, just to prove my point,
but as with every file including program, I get weird readings

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
  #include <iostream>
#include <list>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	list<int> a;
	char secIndex[9][9] ;
	ifstream infile;
	ofstream outfile,outfile1;
	char c;
	int delCount = 0;
	int i =0;
	int j =0;

	infile.open("data.txt",ios::beg);
	cout<<"input file was opened"<<endl;

	outfile.open("SecondaryIndex.txt", ios::cur);
		cout<<"output file 1 was opened"<<endl;
		outfile1.open("LabelId.txt", ios::cur);
		cout<<"output file 2 was opened"<<endl;

	while(!infile.eof())
	{
		
		infile.seekg(1,ios::cur);
		infile>>c;
		if(c == '|')
		{
			cout << " found | "<<endl;
			delCount++;
		}
		if (delCount % 3 == 0)
		{
			while ( c != '|')
			{
				outfile<<c;
				infile>>c;
			secIndex[i][j] = c;
			outfile<<c;
			j++;
			cout<<secIndex[i][j];
			}
			i++;
		}

	}
	
}


I added random values for the data.txt file to see the results:
track1|song1|artist1|album1|
track2|song2|artist2|album2|
track3|song3|artist3|album3|


the first letter of each new word was missing and the remining letters were printed twice, althu I restricted the program to write the letters after the 3rd "|" only, all of them showed up.

General question:
Am I doing the right thing to get specific data from the files ?
EDIT: you do not need to seek. Everytime you do infile >> c, the pointer advances to the next positon
Last edited on
also, if you are trying to make a database like file then I recommend using direct access files

http://cee-ux49.cee.illinois.edu/cee490/public_html/pdfs_vgs/aL23_Direct_Access_Files.pdf

they are great, just use a simple hash and direct access.
Use std::getline() to read character delimited strings from an input stream.

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
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::istringstream stm( "track1|song1|artist1|album1|\n"
                             "track2|song2|artist2|album2|\n"
                             "track3|song3|artist3|album3|\n"
                             "track4|song4|artist4|album4|\n" ) ;

    std::string track ;
    std::string song ;
    std::string artist ;
    std::string album ;
    constexpr char delimiter = '|' ;

    while( std::getline( stm, track, delimiter) &&
            std::getline( stm, song, delimiter) &&
            std::getline( stm, artist, delimiter) &&
            std::getline( stm, album, delimiter) &&
            stm.ignore( 1000, '\n' ) )
    {
       std::cout << "track: " << track << "\nsong: " << song
                  << "\nartist: " << artist << "\nalbum: " << album << "\n\n" ;
    }
}


http://ideone.com/Dz9yrM
Topic archived. No new replies allowed.