| | |
Storing file data in an array?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2006
Posts: 67
Reputation:
Solved Threads: 0
How do I read data such as song titles from a file and store them in an array?
I have:
I have:
C++ Syntax (Toggle Plain Text)
const int SIZE = 50; int main() { char theFile[SIZE]; cout << "Please enter a file name: " << endl; cin.getline(theFile, SIZE); ifstream theFile(" "); ...
Last edited by matrimforever; Nov 26th, 2006 at 7:16 pm.
•
•
Join Date: Sep 2006
Posts: 67
Reputation:
Solved Threads: 0
Hmm,
Think I figured it out, and yes i have to use an array not string:
Think I figured it out, and yes i have to use an array not string:
C++ Syntax (Toggle Plain Text)
const int SIZE = 50; int main() { char theFile[SIZE]; ifstream userData; cout << "Please enter a file name: " << endl; cin.getline(theFile, SIZE); userData.open(theFile);
>>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.
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.
•
•
Join Date: Sep 2006
Posts: 67
Reputation:
Solved Threads: 0
Hmm, how would i use isalpha(aChar) ?
like:
like:
C++ Syntax (Toggle Plain Text)
int main() { char theFile[SIZE]; ifstream userData; cout << "Please enter a file name: " << endl; cin.getline(theFile, SIZE); userData.open(theFile); int i = 0; while(i < SIZE && isspace(theFile[i])) { i++; } if (i == SIZE) return 0; std::cout << (char)toupper(theFile[i++]); std::cout << (char)isalpha(theFile[i++]);
Last edited by matrimforever; Nov 26th, 2006 at 9:28 pm.
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.
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.
•
•
•
•
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?
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
char titles[MAXTITLES][MAXTITLELENGTH]; char name[MAXTITLELENGTH]; int nTitle = 0; while( infile.getline(name,MAXTITLELENGTH)) { strcpy(titles[nTitle++],name); }
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.
•
•
Join Date: Sep 2006
Posts: 67
Reputation:
Solved Threads: 0
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?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; const int SIZE = 50; const int MAXTITLES = 10; const int MAXTITLELENGTH = 50; int main() { char theFile[SIZE]; char songs[MAXTITLES][MAXTITLELENGTH]; int nTitle = 0; cout << "Please enter a file name: " << endl; cin.getline(theFile, SIZE); ifstream userData; userData.open(theFile); while (userData.getline(theFile, MAXTITLELENGTH)) { strcpy(songs[nTitle++], theFile); } cout << songs; cin.get(); system("PAUSE"); return 0; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Problem with program involving classes
- Next Thread: How to generate Graphics in Dev c++
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






