text file destination from an array

This is a discussion on text file destination from an array within the C++ Programming forums, part of the General Programming Boards category; hi, i was wondering does anyone know how i would reference an array of file names so that i can ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    42

    text file destination from an array

    hi,

    i was wondering does anyone know how i would reference an array of file names so that i can reuse a method i have written with several files, for example:

    Code:
    for(int i = 0; i < 3; i++)
    {
    string files[] = {"file1.txt", "file2.txt", "file3.txt", "file4.txt"};
        ifstream inputFile;
    	inputFile.open(files[i]);
    
    
    continue code.....
    }end

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    Read about open? open - C++ Reference

    void open ( const char * filename,
    ios_base::openmode mode = ios_base::in | ios_base::out );

    Open file
    Opens a file whose name is s, associating its content with the stream object to perform input/output operations on it. The operations allowed and some operating details depend on parameter mode.
    It doesn't matter where the const char * argument comes from.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    i did read about it, theres not very much reading to do, i just dont understand it. so i cannot use an array of strings?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    You can use std::strings, but you have to do something like

    file.open ( str.c_string() );

    c_string being a std::string method you should read about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pass contents of text file to 2d array
    By darksifer in forum C Programming
    Replies: 0
    Last Post: 11-22-2010, 11:45 AM
  2. Real problems reading from a text file into an array
    By cherryduck in forum C Programming
    Replies: 11
    Last Post: 11-04-2009, 09:51 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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