File stream as function parameter
-
Hi, I need to parse text files to search some keywords. I split the search process into many different functions (because they search for different contents). So I thought I could open the file just once and then pass the file stream as a function parameter. In debug mode, the app runs, but in release mode it crashes. Can someone explain me why? Here is a sample of the code: void F1 (FILE* source) { ... } void F2 (FILE* source) { ... } void main() { FILE* source; source = fopen("myfile.txt","r"); F1(source); F2(source); }
-
Hi, I need to parse text files to search some keywords. I split the search process into many different functions (because they search for different contents). So I thought I could open the file just once and then pass the file stream as a function parameter. In debug mode, the app runs, but in release mode it crashes. Can someone explain me why? Here is a sample of the code: void F1 (FILE* source) { ... } void F2 (FILE* source) { ... } void main() { FILE* source; source = fopen("myfile.txt","r"); F1(source); F2(source); }
-
Hi, I need to parse text files to search some keywords. I split the search process into many different functions (because they search for different contents). So I thought I could open the file just once and then pass the file stream as a function parameter. In debug mode, the app runs, but in release mode it crashes. Can someone explain me why? Here is a sample of the code: void F1 (FILE* source) { ... } void F2 (FILE* source) { ... } void main() { FILE* source; source = fopen("myfile.txt","r"); F1(source); F2(source); }
Nothing is wrong in above code. may be there is any thing wrong in your function that might be reason for failure in release. anyway,Try to debug the Program,you may yourself find the error. ----------------------------- "I Think this Will Help" ----------------------------- Alok Gupta visit me at http://www.thisisalok.tk
-
Hi, I need to parse text files to search some keywords. I split the search process into many different functions (because they search for different contents). So I thought I could open the file just once and then pass the file stream as a function parameter. In debug mode, the app runs, but in release mode it crashes. Can someone explain me why? Here is a sample of the code: void F1 (FILE* source) { ... } void F2 (FILE* source) { ... } void main() { FILE* source; source = fopen("myfile.txt","r"); F1(source); F2(source); }
fopen()
returnsNULL
on error, if you go and dereference aNULL
pointer, that will crash. You need error checking. ;) --Mike-- LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ | You Are Dumb -
Hi, I need to parse text files to search some keywords. I split the search process into many different functions (because they search for different contents). So I thought I could open the file just once and then pass the file stream as a function parameter. In debug mode, the app runs, but in release mode it crashes. Can someone explain me why? Here is a sample of the code: void F1 (FILE* source) { ... } void F2 (FILE* source) { ... } void main() { FILE* source; source = fopen("myfile.txt","r"); F1(source); F2(source); }
Hi friend, I tried making a program and i got it worked for both release and debug modes I don't know whats the problem in your code, but for ensurance here is my code and you can compare:
#include void f1(FILE *f) { while (!feof(f)) { char s[256]; fgets(s,255,f); printf("%s",s); } } void f2(FILE *f) { while (!feof(f)) { char s[256]; fgets(s,255,f); printf("%s",s); } } main() { FILE *file; file=fopen("c:\\dlls.txt","r"); f1(file); f2(file); fcloseall(); }
Here are some advices: 1. may be you should check other parts of your program 2. you should use the object-oriented new C++ classes like ifstream 3. may be if two threads are trying to access the same file simaltanously a failure will occur. 4. you should check the return values from the functions like fopen and others to locate the bug. I dont know if this helps, but it is all i can do for you friend. Yours, Mohammad a candle looses nothing by lighting another candle -
Hi friend, I tried making a program and i got it worked for both release and debug modes I don't know whats the problem in your code, but for ensurance here is my code and you can compare:
#include void f1(FILE *f) { while (!feof(f)) { char s[256]; fgets(s,255,f); printf("%s",s); } } void f2(FILE *f) { while (!feof(f)) { char s[256]; fgets(s,255,f); printf("%s",s); } } main() { FILE *file; file=fopen("c:\\dlls.txt","r"); f1(file); f2(file); fcloseall(); }
Here are some advices: 1. may be you should check other parts of your program 2. you should use the object-oriented new C++ classes like ifstream 3. may be if two threads are trying to access the same file simaltanously a failure will occur. 4. you should check the return values from the functions like fopen and others to locate the bug. I dont know if this helps, but it is all i can do for you friend. Yours, Mohammad a candle looses nothing by lighting another candleOk, you're right, I'm a dope! :zzz: It works. But I discovered that the real problem is in variables declaration. I'm using fscanf instead of fgets because I need to read a text and analyze each word: FILE* source; char* word = new char[512]; fscanf (source,"%s",word) So, fscanf needs a char* initialised this way, otherwise it won't work. But the pointer initialisation crashes the program. Any suggestion? Thanks
-
Ok, you're right, I'm a dope! :zzz: It works. But I discovered that the real problem is in variables declaration. I'm using fscanf instead of fgets because I need to read a text and analyze each word: FILE* source; char* word = new char[512]; fscanf (source,"%s",word) So, fscanf needs a char* initialised this way, otherwise it won't work. But the pointer initialisation crashes the program. Any suggestion? Thanks
hmmmmmm your code works on my machine. i feel that there is something wrong, but i cant figure it out. but here is a suggestion: are u sure that scanf places a null-terminating character at the end of the buffer? if it does not, initialize the buffer to zeros. Thats all i have my apologises, Mohammad