Storing file data in an array?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Storing file data in an array?

 
0
  #1
Nov 26th, 2006
How do I read data such as song titles from a file and store them in an array?
I have:
  1. const int SIZE = 50;
  2. int main()
  3. {
  4. char theFile[SIZE];
  5. cout << "Please enter a file name: " << endl;
  6.  
  7. cin.getline(theFile, SIZE);
  8. ifstream theFile(" ");
  9.  
  10. ...
Last edited by matrimforever; Nov 26th, 2006 at 7:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Storing file data in an array?

 
0
  #2
Nov 26th, 2006
Hmm,
Think I figured it out, and yes i have to use an array not string:
  1. const int SIZE = 50;
  2. int main()
  3. {
  4. char theFile[SIZE];
  5. ifstream userData;
  6.  
  7. cout << "Please enter a file name: " << endl;
  8. cin.getline(theFile, SIZE);
  9. userData.open(theFile);
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Storing file data in an array?

 
0
  #3
Nov 26th, 2006
Okay still need help then alphabetizing the input. We didn't really go over this, and I couldn't find much in the text. How do I organize the input alphabetically?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Storing file data in an array?

 
0
  #4
Nov 26th, 2006
>>How do I organize the input alphabetically?
there are at least two ways I can think of:
1. use a linked list, insert the new name in alphabetical order. The program will have to search all the nodes in the list to find the right spot.
2. similar to #1 above, but use a dynamically expanding array of strings instead of a linked list.
3. Find out how many names are in the file then allocate an array of pointers of that size. Read each string into the array without regard to alphatetizing. After file has been read sort the array using on of many sorting algorithms.

#3 above is the one I would prefer because I think it is the fastest. Not very much searching involved, which can be pretty slow with large arrays.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Storing file data in an array?

 
0
  #5
Nov 26th, 2006
Hmm, how would i use isalpha(aChar) ?

like:
  1. int main()
  2. {
  3. char theFile[SIZE];
  4. ifstream userData;
  5.  
  6. cout << "Please enter a file name: " << endl;
  7. cin.getline(theFile, SIZE);
  8. userData.open(theFile);
  9.  
  10. int i = 0;
  11. while(i < SIZE && isspace(theFile[i]))
  12. {
  13. i++;
  14. }
  15. if (i == SIZE)
  16. return 0;
  17. std::cout << (char)toupper(theFile[i++]);
  18. std::cout << (char)isalpha(theFile[i++]);
Last edited by matrimforever; Nov 26th, 2006 at 9:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Storing file data in an array?

 
0
  #6
Nov 26th, 2006
variable theFile contains the name of a file on disk, it does not contain the information that is in the file. the open function will just open up the file so that the program can read it -- the open function does not actually read the file. To do that you need to either use the stream's extraction operator >> or call its read() function. What is the purpose of all that code after the open() function? It appears to be completly useless.

isalpha() just checks to see if a character is alphabetic -- that is if the character is one of 'a' to 'z' or 'A' to 'Z'. any other character will fail the test.
Last edited by Ancient Dragon; Nov 26th, 2006 at 9:54 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Storing file data in an array?

 
0
  #7
Nov 29th, 2006
It is part of what else I have to do, such as capitalize the first letter. I was hoping to get the alphabetical part right, but obviously I was wrong.

Can I use:

while (theFile >> aSong)
{
cout << "Songs: " << aSong);
}
Last edited by matrimforever; Nov 29th, 2006 at 10:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Storing file data in an array?

 
0
  #8
Nov 29th, 2006
Hmm, I am supposed to use a two dimensional array, which of course I'm not sure how to do except i need to use ascii maybe?
Last edited by matrimforever; Nov 29th, 2006 at 10:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Storing file data in an array?

 
0
  #9
Nov 29th, 2006
Originally Posted by matrimforever View Post
Hmm, I am supposed to use a two dimensional array, which of course I'm not sure how to do except i need to use ascii maybe?
here is the 2d character array. define MAXTITLES and MAXTITLELENGTH whatever value you wish.
  1. char titles[MAXTITLES][MAXTITLELENGTH];

after opening the file, enter a loop and read each line until end-of-file. If song titles contain spaces you will have to use infile.getline(). Something like this:
  1. char titles[MAXTITLES][MAXTITLELENGTH];
  2. char name[MAXTITLELENGTH];
  3. int nTitle = 0;
  4. while( infile.getline(name,MAXTITLELENGTH))
  5. {
  6. strcpy(titles[nTitle++],name);
  7. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Storing file data in an array?

 
0
  #10
Nov 30th, 2006
Okay I get this to compile, but I must not be converting the file to 'theFile' correctly, because when I enter the file name it just hangs. What have i got wrong?

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5. const int SIZE = 50;
  6. const int MAXTITLES = 10;
  7. const int MAXTITLELENGTH = 50;
  8.  
  9. int main()
  10. {
  11. char theFile[SIZE];
  12. char songs[MAXTITLES][MAXTITLELENGTH];
  13. int nTitle = 0;
  14. cout << "Please enter a file name: " << endl;
  15. cin.getline(theFile, SIZE);
  16. ifstream userData;
  17. userData.open(theFile);
  18.  
  19. while (userData.getline(theFile, MAXTITLELENGTH))
  20. {
  21. strcpy(songs[nTitle++], theFile);
  22. }
  23.  
  24. cout << songs;
  25.  
  26. cin.get();
  27. system("PAUSE");
  28. return 0;
  29. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC