Text Files

This is a discussion on Text Files within the C++ Programming forums, part of the General Programming Boards category; I have a program that reads from several text files in a folder. What I don't understand is why it ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    10

    Text Files

    I have a program that reads from several text files in a folder. What I don't understand is why it reads data from one file, but not another, when they are exactly the same except for the data. Can anyone give me any clue as to what might cause this?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Not without seeing the code you're using to read the files.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    My best guess: you're reusing the ifstream and you're not calling clear() when re-opening. Thus, the eofbit stays set and all read operations on the later files fail.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    10

    Here is the code

    Here is the code that opens a file. I have several text files, some of them work and others don't. Is there some kind of limit on the number of files a program can open?

    Code:
    fstream f;
    char* temp;
    int array[20][20];
    f.clear();
    f.open("C:\\BC5\\BIN\\COEF\\SCREENS\\FIRSTENEMY.TXT", ios::in);
    if(!f)
    { moveto(200, 300);
       outtext("Cannot open file.");
       getch();
       return ' ';
    }
    for (int j = 0; j < 20; j++)
    { for (int i = 0; i < 20; i++)
       { f.getline(temp, 4, ' ');
          scan(temp, array, i, j);
        }
    }
    f.close();

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes, but you seem to be closing the file just fine. The limit only applies to how many files can be open at the same time.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You don't appear to be allocating any memory for temp.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    Quote Originally Posted by Daved View Post
    You don't appear to be allocating any memory for temp.
    Is there anything special I have to do to allocate memory other than declaring it as a normal variable?

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    It's not a "normal" (i.e. local) variable, it's a pointer, so you need to allocate a chunk of memory for it to point to. Ex.
    Code:
    char* temp = new char[80];
    ...
    delete [] temp;
    temp = NULL;

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Code:
    fstream f;
    char* temp;
    int array[20][20];
    
    f.clear();
    f.open("C:\\BC5\\BIN\\COEF\\SCREENS\\FIRSTENEMY.TXT", ios::in);
    
    if(!f)
    {
    	moveto(200, 300);
    	outtext("Cannot open file.");
    	getch();
    	return ' ';
    }
    
    for (int j = 0; j < 20; j++)
    {
    	for (int i = 0; i < 20; i++)
    	{
    		f.getline(temp, 4, ' ');
    		scan(temp, array, i, j);
    	}
    }
    
    f.close();
    This indentation looks better and is more consistent; something to keep in mind writing readable code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Is there anything special I have to do to allocate memory other than declaring it as a normal variable?
    There's no need for dynamic memory allocation here, so I would allocate it as an array of the size you need. Your call to getline sets a maximum of 4 characters that can be read in. I don't remember whether that includes the terminating null or not (you should know find out... it's important). But to be safe, I'll allocate 5 characters in my example:
    Code:
    char temp[5];

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > f.open("C:\\BC5\\BIN\\COEF
    You should also consider having a separate projects directory for all your own work.

    If you've set your compiler up properly, it shouldn't matter where everything is.

    As it stands, you're one upgrade / patch / mistake away from losing stuff.
    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.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    I've tried your suggestions here, but it still will not open FIRSTENEMY.TXT, although it will open other files.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Now is BC5 something like the 32-bit version, or is it an older version which only understands 8.3 filenames (which your's isn't).

    What about the short equivalent name of perhaps "FIRSTE~1.TXT"
    Though do "dir /x" to find out the actual short name.

    If this is the case, perhaps you might consider this to be yet another reason to leave the past behind.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Reading text files?
    By Kate in forum C Programming
    Replies: 3
    Last Post: 06-30-2006, 01:22 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  4. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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