Hi peeps i've got an problem with space and tabs in a text file.

i've build an app that reads a file and creates an array out of every line.

this works just fine but then I noticed that the array is cut of after every space and/or tabular. it just jumps to the next line.
is there a way to solve this issue

//Main.cpp

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

using namespace std;
int i;
int x;
int main(int argc, char *argv[])
{  
    string dataFile;
    string myArray;
    ifstream fin;
    vector<string> myArray_list;
            
    dataFile = "c:\\permissions.dat";
    
    fin.open(dataFile.c_str());
    if (fin.fail())
    {
        cout << "Error reading file! ";
        return 0;
    }    
    i = 0;
    while( fin >> myArray){
      myArray_list.push_back(myArray);
      i++;
      }

    for ( x = 0; x < i; x++ ) {
        cout << myArray_list[x] << endl;    
        }
       
    system("PAUSE");
    return EXIT_SUCCESS;
}

And the text file i've made is a bit xml stylisch... not quite the same though :)

//Permissions.dat

<FILE_HEADER>
	<File_name>Permissions.dat</File_name>
	<File_owner>My Name</File_owner>
</FILE_HEADER>

<DB_TABLE>
	<TABLE_HEADER>
		<Table_name>Permissions</Table_name>
		<Table_owner>MYUSERNAME</Table_owner>
		<Table_nof>5</Table_nof>
		<Table_nor>3</Table_nor>
	</TABLE_HEADER>
	
	<TABLE_CONTENT>
		<Table_row><field>Name</field><field>UserName</field><field>Password</field><field>Function</field><field>Access Level</field></Table_row>
		<Table_row><field>MY NAME</field><field>MYUSERNAME</field><field>md5hashed</field><field>Backup IC'er</field><field>99</field></Table_row>
		<Table_row><field>Test User</field><field>test123</field><field>md5password</field><field>Beta tester</field><field>10</field></Table_row>
	</TABLE_CONTENT>
</DB_TABLE>

and this is the result:

<FILE_HEADER>
<File_name>Permissions.dat</File_name>
<File_owner>My
Name</File_owner>
</FILE_HEADER>
<DB_TABLE>
<TABLE_HEADER>
<Table_name>Permissions</Table_name>
<Table_owner>MYUSERNAME</Table_owner>
<Table_nof>5</Table_nof>
<Table_nor>3</Table_nor>
</TABLE_HEADER>
<TABLE_CONTENT>
<Table_row><field>Name</field><field>UserName</field><field>Password</field><fie
ld>Function</field><field>Access
Level</field></Table_row>
<Table_row><field>MY
NAME</field><field>MYUSERNAME</field><field>md5hashed</field><field>Backup
IC'er</field><field>99</field></Table_row>
<Table_row><field>Test
User</field><field>test123</field><field>md5password</field><field>Beta
tester</field><field>10</field></Table_row>
</TABLE_CONTENT>
</DB_TABLE>
Press any key to continue . . .

Goal is to homebrew a multi user database.
this is just one of my first steps.

any help is appreciated.

Thanks in advanced

>>fin >> myArray

The >> operator will stop reading at the first space or tab. If you want the entire line then use getline(fihn.myArray);

Great: edited the script

i = 0;
    while(getline(fin, myArray)){
      myArray_list.push_back(myArray);
      i++;
      }

Problem solved.

thnx again for you quick and helpfull reply!

This article has been dead for over six months. Start a new discussion instead.