so, if im understanding correctly, instead of trying to mess around with file pointers, i should load all of the files into the program and handle them internally?
so, if im understanding correctly, instead of trying to mess around with file pointers, i should load all of the files into the program and handle them internally?
The point is that you're trying to make a copy of the file handle and expect only one handle to be modified.
You need to understand how files work, that's all. Consider reading the reply below.
Yes, foo2 makes a local copy of the data and assigns it to the local copy (sorry, I actually forgot to modify pLocalStruct instead of pStruct; it was a typo - of course both functions should modify pLocalStruct instead of pStruct).
So, it's before foo1 -> 50, after -> 1, before foo2 -> 50, after foo2 -> 50.
What you tried to do in your code is the same as foo1, so the both points pointed to the same data which was modified by the file functions.
Now, I'm not suggesting you should make a copy of the FILE structure. No, the solution to your problem is either opening the file twice (it's valid to have two or more handles to the same file) or simply rewind the position in the file to the beginning again after you read.
take it theres no easy way to rewind other than (for howmanytimestherehasbeenanoperation)pointer--; ? how can the number of pointer operations be counted?
You rewind a file using the aptly-named function rewind(fileptrgoeshere). It sets the current file read position to the beginning of the file.
apt indeed, im asumming thats called after each function call for the pointer?
ok, new code enclosed, its working. Thankyou for all your help, I'll continue posting code iterations.
one quick question, can the rewinds be done within the function calls i.e rewind(ip) ?
from that response i assumme that file pointers are kinda global?
unrelated question : fgets takes the entire line including the newline character. Any easy way of dropping the last character? i tried adding a \b after each %s in the final printf but that was gonna be a long shot anyway.
You take the return value from a function, how can that be global? Unless it's something else you're referring to?
http://cboard.cprogramming.com/showp...5&postcount=17unrelated question : fgets takes the entire line including the newline character. Any easy way of dropping the last character? i tried adding a \b after each %s in the final printf but that was gonna be a long shot anyway.
ok, that got rid of the new line but now its overwriting the following characters, eg
string1="banana\n"
string2="orange\n"
after running that if statement on both and printffing them both separated by a space you end up with someting like
bannge
:S
i moved u code you suggested from the link into the getword function
the printf statement isCode:const char *getword(FILE* file, int max) { FILE *ip; ip=file; int i,j,size; char c; char buffer[20]; //printf("random %d\n",rand()); //printf("wordno %d\n",(getword_no(ip))); j=(rand()%max); //this restricts the random number to within the length of the file //printf("Random number=%d",j); for(i=0;i<j;i++) { fgets(buffer,sizeof(buffer),ip); //printf("getword\n"); } fgets(buffer,sizeof(buffer),ip); size=strlen(buffer); printf("Length:%d, String: %s",size, buffer); if (buffer[size - 1] == '\n') buffer[size - 1] = '\0'; return buffer; }
Code:printf("\nA %s %s \n--------------------\n A %s %s %s %s the %s %s \n %s the %s %s 's \n The %s %s %s a %s %s\n" ,adjective[0],noun[0] ,adjective[0],noun[0],verb[1],preposition[0],adjective[1],noun[1] ,adverb[0],noun[0],verb[1] ,noun[1],verb[2],preposition[1],adjective[2],noun[2]);
and example output is
Code:cup -------------------- heatclear 's celply inkunnyt