Reading from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • p vs np
    New Member
    • Mar 2009
    • 26

    Reading from a file

    Hi.

    I'm developing a program on pattern recognition.

    In it, I accept two input files (containing strings) from the user through the command line.

    The input files, say input1.txt and input2.txt, are passed to a function processFiles(), which generates permutations of these strings and stores the resultant permutations in two new, generated files, say gen_file1 and gen_file2.

    Immediately after the processFiles() routine is called, I continue by reading gen_file1 and gen_file2.

    Doing this is resulting in some funny behavior. I get a run time error sometimes and sometimes I don't get any output.

    The construct of my program is something as follows:

    Code:
    int main(int argc, char* argv[])
    {
        ....
        processFiles(argv[1], argv[2])
        FILE* file1 = fopen("gen_file1","r");
        FILE* file2 = fopen("gen_file2","r");
        ...
        ...
        processFiles(char* input1, char* input2)
        {
           FILE* file1 = fopen(input1,"r");
           FILE* file2 = fopen(input2,"r");
           /* Some operations */
           FILE* file3 = fopen("gen_file1","w+");
           FILE* file4 = fopen("gen_file2","w+");
           /*Write operations..*/
        }
    }
    I suspect that the compiler is performing the functions in a parallel fashion, i.e, one part is branching out to compute the processFiles() routine and the other is continuing with the serial execution of main().

    In order to confirm my suspension, I did the two operations separately, i.e, i first called processFiles() through one program and then ran another program opening the generated files, and it works fine.

    Any workaround for this situation? Anyway I can synchronize the operations after the processFiles() call?
    Or is it something else, the problem?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I suspect that the compiler is performing the functions in a parallel fashion, i.e, one part is branching out to compute the processFiles() routine and the other is continuing with the serial execution of main().
    On what! basis? Unless you started more than 1 thread your program is single threaded and the code execution path follows, what should be a clear route.

    if you started more than 1 thread it is your responsibility to ensure that they do not interfere with each others operations.

    You code structure is certainly not like that because that wouldn't compile, you can't define functions inside other functions.

    Also that code is useless for diagnosing your problem, all it shows is that you opened some files. It is more likely your behaviour is caused by some sort of undefined behaviour the code has invoked or by poor assumptions in accessing and reusing data.

    However none of that is diagnosable from the posted code.

    Check the variables used by the code reading gen_file1 and gen_file2 to see if any assumptions are made about their initial values which aren't true or may not be true.

    Comment

    • p vs np
      New Member
      • Mar 2009
      • 26

      #3
      Well, to be honest, it was quite stupid of me to suggest anything of the sort that I did!

      I guess it was just the frustration over unsuccessful debugging which resulted in the "hypothesis " of my theory!

      A few of hours of good sleep and I find out the little bug and squish it out..

      It so happened that I hadn't closed the FILE pointers file3 and file4 before I exited the routine processFiles(). As a result, the files were being generated after main() had completed execution. Two lines in, and they were generated when processFiles() was exited, thereby solving the problem.

      Thanks for the help.

      Cheers.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        It is to your advantage to check the return values from standard library functions. Most likely the fopen's in main() were failing. Had you trapped those errors you would have saved yourself a lot of debugging time.

        Comment

        Working...