d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Reading A Txt File In & Storing It In A Variab > Help C++
Add Reply New Topic New Poll
Member
Posts: 625
Joined: Nov 18 2009
Gold: 23.95
Apr 24 2013 03:52pm
Need help for a function that reads a txt file and stores each line it reads into a variable.
What would be better to use for the variables: pointers or references? In my program I've declared the variables in main & .h file and passing them to the function.
There is 9 lines that the .txt file then it gives new data to be filled into the variables.
Heres what I've come up with so far but the output goes "Unable to read" line.
NOTE: Using namespace standard, declare the function in a .h file and have it the file correctly included in main.cpp and other cpp file.

Code

void readFile(string var1, string var2) //I know var3-9 is passed. I have them there for an example purpose
{

ifstream datafile ("file.txt");

if (datafile.is_open()) //Opens the txt file
{
 while(datafile.good())
 {
  getline(datafile, var1);
  getline(datafile, var2);
                       getline(datafile, var3); //var3-9 is just for display, i know it isn't passed.
                       getline(datafile, var4);
                       getline(datafile, var5);
                       getline(datafile, var6);
                       getline(datafile, var7);
                       getline(datafile, var8);
                       getline(datafile, var9);
 
                      cout << var1 << endl;   // Just a test to see if it worked
                      cout << var2 << endl;  //  ^
 }

 datafile.close(); //Closes the txt file after reading in the txt
}

else cout <<"\nUnable to open the data file!\n"; //Reports if unable to read the file to the user
}


Please and Thank you.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 24 2013 03:57pm
pointer to a string array. not only will it clean up your code it will be easier to work with.

This post was edited by AbDuCt on Apr 24 2013 03:57pm
Member
Posts: 625
Joined: Nov 18 2009
Gold: 23.95
Apr 24 2013 05:01pm
Now I'm having a problem with the arrays being uninitialized.

heres how i have them in main:
string *var1[70];

then in main function its:
readFile(*var1);

prototype file & function :
void readFile(string *var1);

then in the function:
Code

void readFile(string *var1, string *var2, string *var3, string *var4,
   string *var5, string *var6, double *var7, double *var8,
    double *var9)
{
int i =0;
ifstream datafile ("file.txt");

if (datafile.is_open()) //Opens the txt file
{
 while(datafile.good())
 {
  getline(datafile, var1[i]);
  getline(datafile, var2[i]);
  getline(datafile, var3[i]);
  getline(datafile, var4[i]);
  getline(datafile, var5[i]);
  getline(datafile, var6[i]);
  datafile >> var7[i];
  datafile >> var8[i];
  datafile >> var9[i];
  cout << var1[i] << endl;
  cout << var2[i] << endl;
 }

 datafile.close(); //Closes the txt file after reading in the txt
}

else cout <<"\nUnable to open the data file!\n"; //Reports if unable to read the file to the user
}


Any Ideas?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 24 2013 05:10pm
Quote (DARK_SPAWN @ Apr 24 2013 07:01pm)
Now I'm having a problem with the arrays being uninitialized.

heres how i have them in main:
string *var1[70];

then in main function its:
readFile(*var1);

prototype file & function :
void readFile(string *var1);

then in the function:
Code
void readFile(string *var1, string *var2, string *var3, string *var4,
   string *var5, string *var6, double *var7, double *var8,
    double *var9)
{
int i =0;
ifstream datafile ("file.txt");

if (datafile.is_open()) //Opens the txt file
{
 while(datafile.good())
 {
  getline(datafile, var1[i]);
  getline(datafile, var2[i]);
  getline(datafile, var3[i]);
  getline(datafile, var4[i]);
  getline(datafile, var5[i]);
  getline(datafile, var6[i]);
  datafile >> var7[i];
  datafile >> var8[i];
  datafile >> var9[i];
  cout << var1[i] << endl;
  cout << var2[i] << endl;
 }

 datafile.close(); //Closes the txt file after reading in the txt
}

else cout <<"\nUnable to open the data file!\n"; //Reports if unable to read the file to the user
}


Any Ideas?


i dont think you grasp the concept of arrays and/or you are posting incomplete code. tale a look at your prototype why are you passing 9 pointers to it. you just need a pointer to a single string array that has enough allocated space to read in your file. also you are not even incrementing your variable `i` inside your while loop.

i would find a debugger and step through this code line by line so you can visually see how you are not doing this right.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll