Like Tree2Likes
  • 1 Post By adeyblue
  • 1 Post By Salem

Array of ifstream

This is a discussion on Array of ifstream within the C++ Programming forums, part of the General Programming Boards category; Hi, What is wrong with this . . . Code: ifstream multiple[MaxFileCount]; CString Names[MaxFileCount]; int Fnames; int j; // Open ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    223

    Question Array of ifstream

    Hi,

    What is wrong with this . . .

    Code:
    ifstream multiple[MaxFileCount];
    CString Names[MaxFileCount];
    int Fnames;
    int j;
    
    
    // Open Multiple Files
    j = 0;
    do
    {
    	multiple[j].open(Names[j], ios::out|ios::out);
    	j++;
    } while(j < Fnames);
    This code continually fails the good() test. I don't know why? Please help :-) Thanks
    Last edited by strokebow; 06-29-2011 at 09:20 AM.

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    Quote Originally Posted by strokebow View Post
    Code:
    ifstream multiple[MaxFileCount];
    This is a bunch of one way streets

    Code:
    multiple[j].open(Names[j], ios::out|ios::out);
    This is you driving the other way

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    223
    Thanks

    I saw that :-) but was having other issues too:

    Code:
    CString temp1;
    j = 1;
    do
    {
    	temp1 = Names[0]; 
    	temp1 += "\\";
    	temp1 += Names[j];
    	multiple[j].open(temp1, ios::in);
    	j++;
    } while(j < (Fnames + 1));
    Running in debug and watching temp1... It gets set as Names[0] and no further amendments are made. The '\' and Names[j] are not concatenated.

    Why is that?

    Cheers.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    > ifstream multiple[MaxFileCount];
    How big is MaxFileCount?

    Compared to most other resources you can have (say bytes of allocated memory), the number of concurrent open files is a very small number on most systems.
    At the low level, you'll encounter an errno value of
    ENFILE The system limit on the total number of open files has been reached.

    You can look up how this might appear in fstream ( as a reason for .bad() )

    You should certainly be checking each open in turn for success.
    strokebow likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    223
    Quote Originally Posted by Salem View Post
    > ifstream multiple[MaxFileCount];
    How big is MaxFileCount?

    Compared to most other resources you can have (say bytes of allocated memory), the number of concurrent open files is a very small number on most systems.
    At the low level, you'll encounter an errno value of
    ENFILE The system limit on the total number of open files has been reached.

    You can look up how this might appear in fstream ( as a reason for .bad() )

    You should certainly be checking each open in turn for success.
    Hi,

    Thanks for your insight Salem.
    MaxFileCount is set at 100. Each file is at most 2KB.

    I have now included a check after each open. I didn't realise there was a system limit on the total number of open files.

    I am trying to combine the data from several text files in to 1 CSV file. My thinking was, I'll open them all, and copy the data from each, line by line, in to one CSV file. I know it isn't the best but hopefully it works ?

    Any ideas why my CString isn't concatenating in the do while loop (Above)?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    Only to wonder why you're not using std::string instead.
    What is CString anyway - something you invented, or something specific to your compiler?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    223

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    The CString class is a .net class not a C++ class. For C++ you should be using std::string. Also the calls to open will require the use of std::string.c_str() member.

    Jim

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    223
    Sorry, I still don't get whats going on...

    Code:
    std::vector< TCHAR > fileNamesBuffer(MaxFileBuffer);
    CString Names[MaxFileCount];
    ...
    Names[j] = Names[j] + fileNamesBuffer[i];
    ...
    CString temp1 = "";
    temp1 = Names[0]; 
    temp1 = temp1 + "\\";
    After, temp1 = Names[0], adding "\\" or "anything" to temp1 will not get added.

    I don't understand why?

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    223
    Putting understanding aside for now...

    I changed my CString to string (C++ style).

    Now when I add the strings or use append() the strings add but there is a 0 in between each add.

    So in the debug watch I'd have:

    [14] 116 't' char
    [15] 0 char
    [16] 92 '\' char

    Whereby the 't' is the last letter of the first string. Then there is a 0 that separates from the "\\" backslash that is suppose to come after the 't'.

    Don't get it? How can I get rid of the 0 pease?

    Cheers

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    Please show the current code that is causing the problems.

    Jim

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    It sounds like you're copying a c string into a std::string the wrong way. The problem is not in append, but with creating the strings in the first place.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ifstream help
    By JustMe in forum C++ Programming
    Replies: 8
    Last Post: 06-03-2007, 02:34 PM
  2. ifstream
    By Wraithan in forum Tech Board
    Replies: 3
    Last Post: 09-24-2006, 01:26 AM
  3. need help with ifstream
    By stillwell in forum C++ Programming
    Replies: 6
    Last Post: 06-20-2005, 09:23 AM
  4. Regarding ifstream...
    By alvifarooq in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2004, 03:12 PM
  5. little help on ifstream
    By ssjnamek in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 12:10 PM

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