A question about file streams.

This is a discussion on A question about file streams. within the C++ Programming forums, part of the General Programming Boards category; Heya, everybody! I have question: How do I work with multiple input files? I've been working on a program for ...

  1. #1
    Registered User Lawn Gnomusrex's Avatar
    Join Date
    Oct 2008
    Location
    Leading lawn gnomes on the path to world domination! ;o)
    Posts
    13

    Talking A question about file streams.

    Heya, everybody!

    I have question: How do I work with multiple input files?

    I've been working on a program for about a week now, and I thought I finaly had it all figured out. It takes a word, then opens up a file, searches it, then if a match is found, it outputs the entire line of that file into a different text file.

    It works perfectly on single files, so I decided I would push myself and try to get it to work with multiple files. It works on the first file, but everything after the first file it goes completely bonkers.

    The actual code is pretty huge, so I won't even try pasting it here. I'll try to see if can give some idea as to what I'm doing.

    The code that now causes it to spazz out is similar to this:

    Code:
    //include files and such...
    
    
    //Functions.
    int search_file();
    
    //Global variables.
    char *file_names[4] = {"File one.txt", "File two.txt", "File three.txt", "File four.txt"};
    
    int file_number = -1;
    
    char word_being_searched_for[30];
    
    
    int main() {
        int i = 0;
        
        cout << "Input a word to search for: ";
        cin.getline(word_being_searched_for, 29);
    
        while(1) {
    
            i = search_file();
            if(i < 0) {
                break;
            }
            if(i > 0) {
                break;
            }
        }
        cout << "Search complete! press enter to exit..." << endl;
        cin.get();
        return 0;
    }
    
    int search_file() {
        char file_to_load[201];
        char parsed_string[30];
        char file_line[1000];
        
        strcpy(file_to_load, "C:\\Users\\Owner\\Documents\\");
        strcat(file_to_load, file_names[++file_number]);
    
        ifstream f_in(file_to_load);
        if(!f_in) {
            return -1;
        }
    
        ofstream f_out("C:\\Users\\Owner\\Documents\\Output.txt");
        if(!f_out) {
            return -1;
        }
    
        while(!f_in.eof) {
             
            f_in.getline(file_line, 999);
            //Code that is not broken that parses each line into individual words.
           
            if(strcmp(word_being_searched_for, parsed string) == 0) {
                f_out << file_line << endl;
            }
        }
        
        if(file_number >= 4) {
            return 1;
        }
        else {
            return 0;
        }
    }

    So I guess what I'm asking is this: would a code like the above work?

    or do I have to individually declare things like this?
    Code:
        ifstream f_in(file_to_be_read);
        ifstream f_in_two(file_to_be_read_two);
        //ect.

    I've never tried to read into more than one file before, so I'm unsure how something like this would work.

    Anyways, thank you for your time!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    This should work, but maybe just not exactly as you expect.
    See, whenever you open a file for writing, it will remove the old file and start a new file. To fix this, pass "ios:ut | ios::append" as second argument to the ofstream constructor. Not sure whether ios:ut is actually required there, to be honest...

    Hope that helps

  3. #3
    Registered User Lawn Gnomusrex's Avatar
    Join Date
    Oct 2008
    Location
    Leading lawn gnomes on the path to world domination! ;o)
    Posts
    13

    Talking

    Alright! Thank you!

    You have no idea how much this was starting to bug me.

    Everything is in perfect working order now, Thank you very much! I appreciate it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. a simple file stream question
    By terracota in forum C++ Programming
    Replies: 12
    Last Post: 07-11-2004, 12:03 AM

Tags for this Thread


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