Reading a file outside of main?

This is a discussion on Reading a file outside of main? within the C++ Programming forums, part of the General Programming Boards category; My programming skills are rusty after too much time away, and I am having a hard time figuring out the ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    Reading a file outside of main?

    My programming skills are rusty after too much time away, and I am having a hard time figuring out the best way to do this.

    I am working on a scanner and I want to read the file name from the command prompt within main. Then I want to call scanner() repeatedly to produce tokens. Scanner will be in a separate scanner.cpp file. What is the best way to do this in order to save the position of the file as I go in and out of the function? Who should actually read the file - main or scanner? It seems that scanner should, but I am a little hazy on how to do this.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's really a design issue. You'll probably want to make your Scanner object (assuming you are actually making an object, and not just a stand alone function) independant. Make it:
    a) Take a file name, and open it for scanning.
    b) Take a file stream and scan it.
    c) Both.
    d) Something else snazzy you can think up.

    It really depends on how you want your program to work. Do you want a Scanner object you can reuse? Or do you just want a parser function for this one time specific task? If you don't care about code reuse, simply make a function that takes a stream and reads from it, doing whatever it is you need it to do.

    If you want your code to be reusable and well, more useful, make it so.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21