Help with get()

Help reading from file with get()

Page 1 of 1

7 Replies - 676 Views - Last Post: 10 May 2010 - 02:59 PM Rate Topic: -----

#1 Guest_Jamie09*


Reputation:

Help with get()

Posted 10 May 2010 - 01:50 PM

Hello everyone,

I find I'm having this consistent problem with reading files...

A simple program a book I'm following had me write uses put() to write a message to a file, sure enough it runs and the file appears. Opening it in Notepad I see the message. No problems thus far...

Then when using this program (which it also had me write) using get() to read a file I'm met again with "Cannot open file..." what am I constantly doing wrong?

I goto the command console and enter in
desktop\get test

I'm met over and over with "Cannot open file..." - I truly can't see what I'm doing wrong - I'd really appreciate some help with this.

Thankyou.

The get() program in quesiton:

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

int main(int argc, char *argv[])
{
  char ch;
  
  if(argc!=2) {
    cout << "Usage: PR <filename>\n";
    return 1;
  }
  
  ifstream in(argv[1], ios::in | ios::binary);
  if(!in) {
    cout << "Cannot open file.\n";
    return 1;
  }
  
  while(in) { // in will be false when eof is reached
    in.get(ch); 
    if(in) cout << ch;
  }
  
  in.close();

  return 0;
}




Is This A Good Question/Topic? 0

Replies To: Help with get()

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Help with get()

Posted 10 May 2010 - 02:30 PM

Does your file name have a space in it?
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Jamie09*


Reputation:

Re: Help with get()

Posted 10 May 2010 - 02:38 PM

View PostJackOfAllTrades, on 10 May 2010 - 01:30 PM, said:

Does your file name have a space in it?


No, the line

get test

"get" is the name of the program and "test" is the file I want it to read, it should simply display the text within the file but can't even open it.
Was This Post Helpful? 0

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with get()

Posted 10 May 2010 - 02:40 PM

Quote

I'm met over and over with "Cannot open file..."
The file needs to be put in the same directory as the current working directory. When you run a program from the commandline, the CWD is considered the directory from which you ran the program.

You said on the commandline, you typed: desktop\get test. That means the program get is on your desktop, and you are in the parent folder (in the commandline). So the current working directory is that parent folder. That's why test cannot be found.

You need to change directory to the desktop, and then run the program from there.

I'm assuming of course that if get program is on Desktop, your test file is on Desktop as well.

This post has been edited by Oler1s: 10 May 2010 - 02:40 PM

Was This Post Helpful? 2
  • +
  • -

#5 Guest_Jamie09*


Reputation:

Re: Help with get()

Posted 10 May 2010 - 02:47 PM

Thanks for the reply, the test folder is indeed on the desktop along with the get program. I'm sorry to sound extremely stupid here, but can you tell me as to how I can switch the directory? - Thankyou.
Was This Post Helpful? 0

#6 Guest_Jamie09*


Reputation:

Re: Help with get()

Posted 10 May 2010 - 02:52 PM

I got it going - I moved the get program and test file to my Documents and Settings folder and can call them as intended now. However, why would they not correctly open on the Desktop if all needed files and programs were present?
Was This Post Helpful? 0

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Help with get()

Posted 10 May 2010 - 02:53 PM

Quote

I'm sorry to sound extremely stupid here, but can you tell me as to how I can switch the directory?
cd <name of directory>

You're not stupid for not knowing things. People aren't born with this information. That said, did you try Googling for "commandline change directory" or other terms? On that note, if you are unfamiliar with how to use the commandline, you should Google for more information on the Windows commandline. The concepts should be mostly relevant regardless of learning resource, although old resources that reflect 90s era commandline or DOS aren't going to be correct down to the detail.
Was This Post Helpful? 1
  • +
  • -

#8 Guest_Jamie09*


Reputation:

Re: Help with get()

Posted 10 May 2010 - 02:59 PM

You're not stupid for not knowing things. People aren't born with this information.

Thanks for that :)

I had googled up "change directory" and "c++ change directory" but didn't fair well. Thankyou again though - I got it working and know how to change directories now - I really appreciate you help.

I'll still go and have a deeper look as you suggest.
Was This Post Helpful? 0

Page 1 of 1