Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. File stream as function parameter

File stream as function parameter

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingquestionannouncement
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vilmer
    wrote on last edited by
    #1

    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); }

    T T M M 4 Replies Last reply
    0
    • V vilmer

      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); }

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      what error does it repport, and what is the code line like ?


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      1 Reply Last reply
      0
      • V vilmer

        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); }

        T Offline
        T Offline
        ThatsAlok
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • V vilmer

          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); }

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          fopen() returns NULL on error, if you go and dereference a NULL pointer, that will crash. You need error checking. ;) --Mike-- LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ | You Are Dumb

          1 Reply Last reply
          0
          • V vilmer

            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); }

            M Offline
            M Offline
            Mohammad A Gdeisat
            wrote on last edited by
            #5

            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

            V 1 Reply Last reply
            0
            • M Mohammad A Gdeisat

              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

              V Offline
              V Offline
              vilmer
              wrote on last edited by
              #6

              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

              M 1 Reply Last reply
              0
              • V vilmer

                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

                M Offline
                M Offline
                Mohammad A Gdeisat
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups