Initiating and reading a txt file

Hi guys.
I need help with reading a .txt file, and converting every line into a string. For instance, file path is D:\file.txt...
What libraries i need, how to open it, and read one line at a time?
Thx in advance, I've been trying for hours now.
Thx, but i read that...

Compiler returns 4 errors:

1>c:\visual studio 2005\projects\nenadzivic-asocijacije\asocijacije\glavni kod.cpp(29) : error C2065: 'ifstream' : undeclared identifier
1>c:\visual studio 2005\projects\nenadzivic-asocijacije\asocijacije\glavni kod.cpp(29) : error C2146: syntax error : missing ';' before identifier 'baza'
1>c:\visual studio 2005\projects\nenadzivic-asocijacije\asocijacije\glavni kod.cpp(29) : error C2065: 'baza' : undeclared identifier
1>c:\visual studio 2005\projects\nenadzivic-asocijacije\asocijacije\glavni kod.cpp(31) : error C2228: left of '.open' must have class/struct/union

The code is like:

void main(){
char pojmovi[40][1000], kontrolni_hit;
int redni,boolean,brojac[8],broj_rundi,broj_ekipa,duzina_runde;
ifstream baza;

baza.open("probni.txt");

for (int i=1;i<=10;i++){
fgets(pojmovi[i],20,baza);
};
.......

...and I included <iostream> and <fstream>;
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <fstream> // include fstream

void main(){

char pojmovi[40][1000], kontrolni_hit;
int redni,boolean,brojac[8],broj_rundi,broj_ekipa,duzina_runde;

// prefix with std:: !
std::ifstream baza;

baza.open("probni.txt");

for (int i=1;i<=10;i++){
fgets(pojmovi[i],20,baza);
};
Thx for this, its getting better i guess, 2 errors now:

1>c:\visual studio 2005\projects\nenadzivic-asocijacije\asocijacije\glavni kod.cpp(38) : error C2664: 'fgets' : cannot convert parameter 3 from 'std::ifstream' to 'FILE *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\visual studio 2005\projects\nenadzivic-asocijacije\asocijacije\glavni kod.cpp(91) : error C2660: 'std::basic_ifstream<_Elem,_Traits>::close' : function does not take 1 arguments
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
You cant use fgets() with a std::ifstream. Maybe have a look at getline()?
http://www.cplusplus.com/reference/iostream/istream/getline/

Your main problem: if you're going to move from C to C++, make the switch completely.
Yes, i agree with Kiana, i guess thats the main problem. Now, can u tell me what sholud i change in the code in order to fix it. 'Cause i have a C book, and i work in C++....
Probably something like this:
 
baza.getline(pojmovi[i], 1000);
Thx. It compiles now with no errors.

But... Sequence

for(int i=1;i<=10;i++){
baza.getline(pojmovi[i],1000);
printf("%s",pojmovi[i]);
};

..doesn't print anything... Any ideas?
How about this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> // for cout
#include <fstream> // include fstream

int main(){

std::string pojmovi[40], kontrolni_hit; // use std::string instead of char array
int redni,boolean,brojac[8],broj_rundi,broj_ekipa,duzina_runde;

// prefix with std:: !
std::ifstream baza;

baza.open("probni.txt");

for (int i=1;i<=10;i++)
  {
     getline(baza,pojmovi[i]); // <--- get line from 'baza' and put it into 'pojmovi[i]'
     std::cout <<pojmovi[i]<<std::endl; // use cout to print std::strings
  };

}


Galik wrote:
void main()
:-)
Last edited on
Galik wrote:
void main()

Hehehe. In my defence I was just copying his code.... ;o)
Nenad Zivic wrote:
..doesn't print anything... Any ideas?

It works for me, perhaps your file is empty?

Also you should count from zero like this:
 
for(int i = 0;i < 10; i++) {

Arrays are indexed from 0, not 1.
Hmmm. My file isn't empty, i double-checked it...

It prints somehing, but it looks like it prits an empty line...
Is this ok?

baza.open("D:\\bazazaasocijacije.txt");

Or there is a special directory where these .txt should be stored?

Null, ty too, i will try your solution also...
Here is the whole code, so take a look...

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
#include <conio.h>
#include <stdio.h>
#include <fstream>
#include <strstream>


int wait(int seconds){
clock_t endwait;
endwait=clock()+seconds*CLOCKS_PER_SEC;
while (clock()<endwait) {

if (_kbhit()){
getch();
return 1;
};

};
return 0;



}

void main(){

std::ifstream baza;

baza.open("F:\\data.txt");



char pojmovi[40][1000], kontrolni_hit;
int redni,boolean,brojac[8],broj_rundi,broj_ekipa,duzina_runde;

for(int i=0;i<10;i++){
baza.getline(pojmovi[i],1000);
printf_s("%s",pojmovi[i]);


};



printf("Unesite koliko rundi zelite da traje igra:\n");
scanf("%d",&broj_rundi);

printf("\nUnesite koliko ekipa ce igrati:\n");
scanf("%d",&broj_ekipa);

printf("\nUnesite trajanje jedne runde u sekundama:\n");
scanf("%d",&duzina_runde);

fflush(stdin);

for (int o=1;o<=broj_ekipa;o++){
brojac[o]=0;
};

for (int l=1;l<=broj_rundi;l++){
for (int k=1;k<=broj_ekipa;k++){
while (1){

printf("Pritisnite <space> pa <enter> za nastavak:\n");
scanf("%c",&kontrolni_hit);
if (kontrolni_hit==' '){
break;
};
};
printf("Ekipa broj %d, %d. runda:\n\n",k,l);
redni=rand()%3+1;//ili sizeof(pojmovi)
printf("%s\n",pojmovi[redni]);//nova rec
fflush(stdin);
for (int j=0;j<=duzina_runde;j++){
if (j%5==0){
printf("Jos %d sekundi...\n",duzina_runde-j);
};
boolean=wait(1);
if (boolean){
redni=rand()%10+1;//ili sizeof(pojmovi)
printf("%s\n",pojmovi[redni]);//nova rec
brojac[k]+=1;
boolean=0;
fflush(stdin);
};
};


};

};

for(int p=1;p<=broj_ekipa;p++){
printf("\nEkipa broj %d osvojila je %d poena\n\n",p,brojac[p]);
};


}
That works for me when I change printf_s("%s",pojmovi[i]); to printf("%s",pojmovi[i]);
I'm glad at least anyone can use it. :D

Joking aside, what editor do you use? I use VS 2005... Do you come up with any rational reason why it's not working on my computer? :D
I use eclipse on Linux :o)

Maybe the file is not where you think it is? Or maybe the file is not readable? Do you have permissions right?
Yes, i checked the path to it... And i do have permissions, I'm working on my PC...
Topic archived. No new replies allowed.