I must make program in c++ that reads unknown number of words from a text file to char arr which after that i will have to sort the words by the first letter by alphabet in a stack.
can any body help me please?
This looks to me like a homework post, which I don't think will get much help here until (maybe) you give us some more of what you already have started...
Remember to use code tags to format your code, if you decide to.
I would be happy to help as long as I know I'm not doing someone's homework... I think many other people here feel the same.
I can not show any code because i have problems with just the beggining of the code
yea of course I can show you my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <fstream>
#include <iostream>
usingnamespace std;
struct elem{
char word[255]; //an array for a single word that can contain up to 256 letters
elem * next; //pointer to the next elem;
}
int main() {
ifstream in("file.txt", ios::out);
}
this is my code. :P:(
my idea is that if a string is made of linked char arrays then I must make a linked list of char arrays.
it is just I do know understand how to use the fscanf funct., because here at cplusplus.com i do not understand the fscanf tutorial and the parameters;
i want to read words (that are separated with a space) with a fscanf function in c++ because with
fin <<
i am reading by char but in that case i must know the number of words in the file and also I can not read all file at time or by line.
please help me on how to use the fscanf or a similar function.
Taken from fscanf documentation on this site: fscanf (pFile, "%s", str);
The "%s" says that the format you want to use for storing the data read from the file is 'string' format. The third parameter is the character array in which you want to store your data. The first parameter, 'pFile', is of type FILE*, and represents the file stream associated with the file you (should) have already opened.
Taken from fscanf documentation on this site:
1 2 3
FILE * pFile;
pFile = fopen ("myfile.txt","w+");
Currently, you are using fstream objects for file manipulation. I do not know of a way to convert fstream objects to FILE* objects (I seem to remember hearing this was not possible).
You may want to change from fstream objects to using FILE* objects. This declares a pointer to a filestream. The second parameter ("w+") means to open the file for reading and writing. For a full list of options, see: http://www.cplusplus.com/reference/clibrary/cstdio/fopen/