Hello, i have one question for reading from file.
I get data from file ulaz.txt and i want to data in there would be stored like

some text;1934
text 2;2342

for now its stored like

sometext 1934
text2 2342

so i want to be able to use space between text and still be able to write that text into one variable called naziv_student.

Thank you in advance, if i am not clear just say and i will do my best to explain.
P.S. Sorry for my bad english

struct student{
    string naziv_student;
    int visina;
};
student studenti[100];
void uzimanjeDat(){
ucitano=true;
ifstream datoteka;
datoteka.open("ulaz.txt");
while(!datoteka.eof()){
datoteka >> studenti[brojac].naziv_student >> studenti[brojac].visina;
brojac++;}
}

I'm not sure what you are asking for, but I think you seem to be interested in preserving your white spaces. Try using getline():

string line[50];
int pos = 0;
int i=0;

while(getline(datoteka, line[i]))
{
     //Go back to the start of the line so you can extract data based on white spaces
     curr_pos = datoteka.tellg();
     char_count = gcount();
     datoteka.seekg(curr_pos - char_count);

     //Now you don't have to parse line[], just read the same line in again
     datoteka >> studenti[i].naziv_student >> studenti[i].visina;
     
    i++;
}

So now you have 'string line[]' which contains all the lines of the file with white spaces preserved.

This code is untested, uncompiled, and may contain small easy to fix errors (specifically, I am concerned about 'off by one' errors associated with my seekg() 'get pointer' placement.)

Thank you for your reply but im sorry to say its not what i asked for.
Right now i have file with data writen like

text1 245
text2 6546

i want to be able to write data to text file like this:

text 1;245
text 2;6546

and then later get data from text file to my struct,

text 1 = naziv_student and 245 = visina.

I hope i cleared now :)

Thank you for your help

Lets say like this.

For now i have to write like:

naziv_student="FirstnameLastname"
 visina=178

i want to be able to write like

naziv_student="Firstname Lastname"
visina=178

text file for now looks like

FirstnameLastname 178

and i want to be it like

Firstname Lastname;178

where ; is separator or something like this, but i dont know how to do it.

So you want to write to the file, yes?

In that case you need to use ofstream

So you would say something like this:

std::osfstream readout;
readout.open("file.txt", iso::app);
readout<<naziv_student<<" "<<";"<<visina<<"\n";

You might also want to explain the nature of the task i.e are you doing an exercise in which you have a txt file with the data

1. firstnamelastname 178

and you need the program to read in from one file and split "firstnamelastname" to "firstname lastname" then write it to another file as firstname lastname ;178?

No, i know i was unclear but i will do my best to explain myself. Lets say i have file data.txt. Inside is written
John Travolta;192

I want to open this file data.txt and write string John Travolta to
string name and number to int height. I dont know how to separate those 2 variables when i open file.

so data.txt looks like

John Travolta;192
Alicia Keys;180

and i want to input those data to my c++ program to 2 separate fields like:

string field[0]="John Travolta";
int height[0]=192;
string field[1]="Alicia Keys";
int height[1]="180";

I hope its clearer now, thank you for your help.

I read what is written there, but i still dont get how to use delimiter :S

Is it something like that ?

void uzimanjeDat(){
ucitano=true;
ifstream datoteka;
string line;
datoteka.open("ulaz.txt");
while(getline(datoteka,line,;)){
datoteka >> studenti[brojac].naziv_student >> studenti[brojac].visina;
brojac++;}
}

getline(datoteka,line,';')

#include <iostream>
#include <string>
using namespace std;


int main()
{
    string name;
    getline(cin,name,';' );
    int height;
    cin>>height;
    cout<<name<<" "<<height;

return 0;
}

input: Iga Wyrwal;169
output: Iga Wyrwal 169

PS: sorry for double posting

you guys dont understand, i just wrote sample, i dont want to enter text from keyboard, i want data to be fetched from .txt file, but thx anyways

what's the difference? This is just reading from a stream (cin, ifstream, etc.).

void uzimanjeDat(){
ucitano=true;
ifstream datoteka;
string line;
datoteka.open("ulaz.txt");
while(getline(datoteka,line,';')){
datoteka >> studenti[brojac].naziv_student;
datoteka >> studenti[brojac].visina;
brojac++;}
}

I tried like u said but it doesnt work. It returns only the number after ; and it saves it to naziv_student which is string :S

Please help me. Thank you

You read something to string line , and then You don't use it. Read again the example I gave you and try to correct your code.

You read something to string line , and then You don't use it. Read again the example I gave you and try to correct your code.

Thank you, you are right, now its working but there is just one small problem, it automaticly inserts newline on begining of every string. How can i solve this ?.
Thank you once again

void uzimanjeDat(){
ucitano=true;
ifstream datoteka;
datoteka.open("ulaz.txt");
string line;
while(getline(datoteka,line,';')){
datoteka >> studenti[brojac].visina;
studenti[brojac].naziv_student = line;
cout<<studenti[brojac].naziv_student<<"  "; // just to test output
brojac++;}
}

I have done it, but tell me if this is ok way to do it or if u have any other idea, thank for all your help.

void uzimanjeDat(){
ucitano=true;
ifstream datoteka;
datoteka.open("ulaz.txt");
string line;
while(getline(datoteka,line,';')){
datoteka >> studenti[brojac].visina;
string::iterator it;
int velicina=line.size();
for(size_t x = 0; x < velicina ; x++)
{if(line.at(x) == '\n'){
			it = line.begin() + x;
			line.erase(it);
			velicina--;	
		}}
studenti[brojac].naziv_student = line;
brojac++;}
}

This shouldn't be this hard.

You want to read from a text file of the format :

Firstname;Lastname;178

where the ';' means the word has ended. And you know you will
always read firstname, then lastname, then the value. Now first
you need to open a file. Of course you know how to do that :

ifstream getFromFile("text.txt");

That opens a file to read. Now you have to read.

First lets say this is the file :

Firstname;Lastname;000
Tony;Montana;100;
Best;Forever;99;
Richard;Jones;98;
World;Hello;97;

You now need to read the file. What you can do is read 1 line into a string. Then have a function find all occurrence of ';' and replace it with a space.

Here is a complete example :

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


using namespace std;

//find wherever ';' is and replace it with ' '
void wordReplace(string& source,char find = ';', char replaceWith = ' ')
{
	size_t pos = 0;

	while(true)
	{		
		pos =  source.find_first_of(find,pos);	 //find the position of ';'
		if(pos == string::npos)
			break;
		source[pos] = replaceWith; //replace ';' with a space
	}

}
int main() 
{	
	ifstream iFile("test.txt"); //open file to read
	
	if(!iFile) return -1; //check if file is opened

	string word = ""; 
	
	vector<string> fullNameWithValue;
	
	while( getline(iFile,word)) {		 //get a line into a string
		wordReplace(word); //replace ';' with ' '
		fullNameWithValue.push_back(word); //add word to out list
	}

	for(size_t i = 0; i < fullNameWithValue.size(); i++) //print out word
		cout<<fullNameWithValue[i]<<endl;

	 	
	return 0;	
}

Thank you, you are right, now its working but there is just one small problem, it automaticly inserts newline on begining of every string. How can i solve this ?.
Thank you once again

#include <limits> //remember to add this

void uzimanjeDat(){
ucitano=true;
ifstream datoteka;
datoteka.open("ulaz.txt");
string line;
while(getline(datoteka,line,';')){
datoteka >> studenti[brojac].visina;
datoteka.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );  //<----this is new: discard '\n'
studenti[brojac].naziv_student = line;
cout<<studenti[brojac].naziv_student<<"  "; // just to test output
brojac++;}
}
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.