0

I want to append data to files if a certain condition is true, else open a new file and keep writing to that file...this goes on in a loop.
This is what I am doing:

FILE* ptr
firstrun=1
***some code***
WHILE (condition)
{
    if(!condition1 && !condition 2)
        something
    else if(condition1 || condition 2)
    {
        write data to file
        if(firstrun)
            FILE* ptr
            fopen a file
            firstrun=0

        ***some code***
        if condition1
            append data to previously opened file
        if condition2
            fclose
            FILE* ptr
            fopen another new file
    }
}

The code as a whole doesn't seem to be working right but the other parts do seem right. The first file created matches the answer, but none of the following files do. I am also creating a whole lot more files, with no idea how much of the data is getting repeated.
Could anyone tell me whether what I have done here is right? I'm not posting the code because it's huge.

Some other cases:
1. If I don't declare FILE* ptr at the top, it doesn't compile because the only other declaration is inside an "if" condition
2. The other way is to use more "if"s which would make the code messier, and I'm not sure if that would work either.

Any help appreciated! Thanks!

fineashell
  • 31
  • 7
  • 1
    This pseudo code isn't helpful at all. Could you please try to break your code down to a small example that shows the behaviour you observe? So that we can help you spot real errors that you have as opposed to *guessing* errors you *might* have? – M Oehm Oct 29 '14 at 21:09
  • Okay...I will in a moment. – fineashell Oct 29 '14 at 21:10
  • Uh..no I can't. It's a college assignment (don't want to be caught taking help), plus the code is very confusing, interconnected and big. It's mainly the replacement mergesort algorithm, with a primary heap writing to output buffer. I want to create runfiles from a bigger input.bin file. Condition 1: outputbuffer < 1000 and condition 2: heapsize > 0. If either are false, I should append data to a previously opened file. I checked, my heapsort works fine. I don't have much confidence in how a FILE pointer operates though – fineashell Oct 29 '14 at 21:24
  • 1
    If you're not supposed to be asking for help, then what makes you think we want to collude with you to cheat? In the end, though, it doesn't matter because we *can't* help you without a much better idea of what your code actually looks like. – John Bollinger Oct 29 '14 at 21:27
  • 1
    Nevertheless, the exercise of breaking down the program to a minimal case that exhibits the problem may be helpful *to you* for identifying the problem. It will make you think about what's going on, and to figure out which bits contribute to it and which don't. – John Bollinger Oct 29 '14 at 21:30
  • Well, a few things: 1) I'm assuming that you want to keep the first file AROUND when you move on to the second. 2) Do you want to keep the first file OPEN when creating a new file? 3) If no to #2, then your pseudo code isn't too far off the mark. You DON'T, however, want to redeclare FILE* ptr inside any of the if statements (or anywhere for that matter) – Don Shankin Oct 29 '14 at 21:34
  • @Don Your assumptions are correct. I tried what you said too, makes no difference. – fineashell Oct 29 '14 at 21:38
  • @fineashell, which part did you try? Since FILE* ptr is already declared at the top, under your condition to open the new file, simply do something like fclose(ptr); ptr = fopen("/some/new/filename", "w"); – Don Shankin Oct 29 '14 at 21:39
  • @John I'll try what you said. Maybe I can confirm which section of my code has a problem. Thanks. – fineashell Oct 29 '14 at 21:39
  • @Don Tried the don't redeclare FILE* ptr inside any if statement. In fact, I tried every combination of declaration of the file pointer as possible. – fineashell Oct 29 '14 at 21:41
  • I will try what you said again. – fineashell Oct 29 '14 at 21:43
  • Yep. No difference. Maybe I'll try breaking down the code so I can post it here. – fineashell Oct 29 '14 at 21:44

1 Answers1

0

I'm not at work and can't compile anything right now, but try something along the lines of

FILE* ptr = NULL
firstrun = true;
while (condition)
{
    if((false == condition1) && (false == condition 2))
    {
        ...
    }
    else    /* Your second condition logic is not needed as written */
    {
        if(true == firstrun)
        {
            ptr = fopen("/some/file/name", "w");
            firstrun = false;
        }

        if (true == condition1)
        {
            fwrite(ptr, ..., ..., ...);
        }
        else
        {
            fclose(ptr);
            ptr = fopen("/some/other/file/name", "w");
        }             
    }
}

Note that your question said IF a condition is true, append to a file, ELSE open another file. Your pseudo code checks for a condition to write, then another condition to open a new file. This doesn't give you the if/else that you're looking for; You can hit any of four possibilities with your original pseudo code:

  1. Neither write to the open file nor open a new one
  2. Write to the open file only
  3. Open a new file only
  4. Write to the open file, AND open a new file
Don Shankin
  • 440
  • 3
  • 10
  • I found a mistake in the other section. Except for the redeclaration in the pseudocode, everything else works fine. – fineashell Oct 30 '14 at 18:49
  • 1
    That's good to hear. Do keep in mind that the logic in the psuedo code still is incorrect if you want a pure if/else relationship regarding writing or opening the file. If it's working as an if/else, it's because the conditions that you're monitoring just happen to make it so for your testing – Don Shankin Oct 30 '14 at 18:55