Forum Thread: Fstream Questions

I'm trying to create a c++ program to read into files without actually opening the file... my question here is fstream. Example:

fstream fileName;

char buffer;
std::cout <<"Enter in file: " <<; std::endl;
std::cin >>fileName; std::endl;

char buffer64
int i;
for (i=63; i>64; i++);
bufferi =filename.getline;

my question here is this possible? If not please tell me what I'm doing wrong. But the point here if I'm just trying to read into a file line by line then wouldn't you need to be able to read each line from the file and for each line then you would go up 1? if this seems confusing you can view my full code here. Please and thanks.

I haven't tried running this code through a compiler, but I'm just wondering if this could be possible..... :)

3 Responses

Yes possible, but you will need to open the file to read its contents. I took a look at your code and noticed a number of other issues (buffer not declared as an array, pB never defined, 2 return statements, the 2nd of which will never be reached, several more). Run it through your compiler and start fixing the errors one at a time, or if you just want to get it working without trying to figure it out on your own, googling "fstream readline" will get you tons of examples. Cheers!

Thank you very much... I will continue working on this code a bit more. :)

I actually created a function for one of my courses a couple years ago. You have to make sure to include the right libraries and also to remove any parameters you don't need. Where fname is the name of the text file you want to open. You also have to make sure that the text file is in your working folder.

Hope this helps.

Code:

void CopyData(string fname, int idNums, string Names, int Balances);
// Copy Data from Text file
void CopyData(string fname, int idNums, string Names, int Balances)
{
fstream g;
g.open(fname, ios::in);
{
int i = 0;
while (true)
{
g >> Namesi >> idNumsi >> Balancesi;
if (g.fail())
break;
++i;
}
}
g.close();
}

Edit: Seems like there is some problems when I pasted the code but the parameters other than fname are arrays and so for each i there are square brackets.

Share Your Thoughts

  • Hot
  • Active