LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2007, 07:53 AM   #1
tinieprotonjam
LQ Newbie
 
Registered: Dec 2006
Posts: 28

Rep: Reputation: 15
Question problem with opening file function in c++


/*I am currently programming in Linux using the g++ compiler. I was trying to make an open function that would open a file and store its contents in a 2d vector. I was successful at doing the said function. I thought I was done with it, but I realized that I can sucessfully call the function once in the prgrogram, whenever I call it again, it would not open any file. So I thought there might be wrong with my implementation, so i just just opening a file and then just output it to the screen, only one file is sucessfully opened and only one output is shown. Here's the code, can anybody help in pinpointing the problem.


here's the code I did, trying to find out what's wrong, I was only prompted to enter the filename once, then the next one somewhat bypassed...
Code:
//try.cpp
#include <iostream>
#include <fstream>
using namespace std;

char filename[20], new_file[20];
fstream is;
int openFile(char file[20]){
char c;
if (is.open (file)){        // open file

  while (!is.eof())     // loop while extraction from file is possible
  {
    c = is.get();       // get character from file
    cout << c;
  }

is.close();           // close file}
else cout<< "error opening file";
return 0;
}


int main () {
  //char c, str[256], newfile[256];
  

  cout << "Enter the name of an existing text file: ";
  cin.get (filename,20);
  openFile(filename);

cout << "Enter again the name of an existing text file: ";
cin.get (new_file,20);
openFile(new_file);
  return 0;
}
here's the output, after compiling this using g++ -o try try.cpp:

[root@192~]# ./try
Enter the name of an existing text file: file01.txt
new file
ito
hahaha˙Enter again the name of an existing text file: [root@192~]

the program will not let me type the filename of the next file that I want to open...
I hope someone would be able to help me....thanks in advance....
 
Old 02-14-2007, 09:40 AM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
ignoring the syntax errors (probably a copy/paste error?), you have a couple logical errors.

a. cin.get leaves the delimiting char on the stream. so after you get the first string the new line is extracted by the second cin.get call leaving you with an empty second string. (see some documentation on istream::get)

b. when you open an fstream, read it to the end, close it, and open it again for reading, you have to call clear() to clear the error flags. otherwise the eof (and other) error flag is still set from the first open, read, eof, close sequence.

PS. a better solution for a. (among others) would be istream::getline()

Last edited by xhi; 02-14-2007 at 09:42 AM.
 
Old 02-14-2007, 09:48 PM   #3
tinieprotonjam
LQ Newbie
 
Registered: Dec 2006
Posts: 28

Original Poster
Rep: Reputation: 15
Thumbs up

Quote:
Originally Posted by xhi
ignoring the syntax errors (probably a copy/paste error?), you have a couple logical errors.

a. cin.get leaves the delimiting char on the stream. so after you get the first string the new line is extracted by the second cin.get call leaving you with an empty second string. (see some documentation on istream::get)

b. when you open an fstream, read it to the end, close it, and open it again for reading, you have to call clear() to clear the error flags. otherwise the eof (and other) error flag is still set from the first open, read, eof, close sequence.

PS. a better solution for a. (among others) would be istream::getline()

regarding the syntax errors, i mistakenly posted the unedited code, but I was able to fix it... secondly, I added the call to clear after closing the file and then used iosteam::getline instead of iostream::get, and the problem was solved... thanks a lot...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help opening file LinuxPimp Linux - General 7 01-26-2007 12:07 AM
opening remove video file problem rbolt168 Linux - Networking 2 11-04-2006 07:50 PM
Xp user opens file via Samba, but file is not locked after opening??? marisdembovskis Linux - Networking 8 07-04-2006 03:30 AM
Adobe Reader 7.0 and Slackware 10.1, problem opening a file that required a plugin houler Linux - Software 0 05-29-2005 03:23 PM
bad problem opening a bin file. dirky Linux - Newbie 1 09-25-2004 10:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration