There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: Reading a file into an array in C++


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
TheRobatron's Avatar
Computer Specs
Senior Member with 417 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
17-Jan-2008, 03:57 PM #1
Solved: Reading a file into an array in C++
I want to read a .txt file (containing all the alphanumeric characters) into a two dimensional array using C++. In the file there are 2 sets of 36 lines, with one character on each line. How can I read the file into a char array?

Here is the code I have so far (that doesn't work):
Code:
bool checkfile () {
     char *a;
     char alphanum[2][36];
     ifstream check;
     check.open("alpha.txt");
     // If the file is open
     if (check.is_open()) {
     cout << "alpha.txt opened successfully.";
     // Read the contents of the file into an array
     // x is for the alphabet number and y is for each character
     for (int x = 1; x < 11; x++) {
         for (int y = 1; y <= 36; y++) {
             getline (check,alphanum[x][y]);
         }
     }
     check.close();
     cout << alphanum;
     // If the file cannot be opened (or doesn't exist)
        } else {
     ...
         }
     return true;
};
Thanks
johnhe's Avatar
Member with 56 posts.
 
Join Date: May 2007
Experience: Advanced
17-Jan-2008, 06:44 PM #2
When indexing an array in C or C++ you need to remember that arrays are zero based. Your loop counts need to start at zero and end when the count reaches the number of elements in the assocated dimension of array.

It's also important to note that ifstream::getline() always places a string terminator at the end of the line. Reading a line with 1 character actually requires 2 characters of storage.

Here is an example of one way to set up the loop to read in all of the characters from the text file:

Code:
     // Read the contents of the file into an array
     // x is for the alphabet number and y is for each character
     for (int x = 0; x < 2; x++) {
         for (int y = 0; y < 36; y++) {

             // Read in the line 
             char Line[2];
             check.getline( Line, 2 );

             // Copy the character to the array
             alphanum[x][y] = Line[0];
         }
     }
One other thing to remember is that cout is expecting a zero terminated string or single character. You should do something like this when displaying the individual characters:

Code:
      // Display the characters in the array
     cout << endl;
     for (int x = 0; x < 2; x++) {
        for (int y = 0; y < 36; y++) {
            cout << alphanum[x][y];
         }
      }
      cout << endl;
Regards,
__________________
John Hensley
www.resqware.com
TheRobatron's Avatar
Computer Specs
Senior Member with 417 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
18-Jan-2008, 03:25 PM #3
Thanks very much for your help - my program works fine now
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 08:26 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.