Compiler or OS bug??

This is a discussion on Compiler or OS bug?? within the C++ Programming forums, part of the General Programming Boards category; I need some urgent help here. This is the (isolated) problem. When I drag a file(s) onto the program's icon ...

  1. #1
    Frantic
    Guest

    Compiler or OS bug??

    I need some urgent help here. This is the (isolated) problem. When I drag a file(s) onto the program's icon to run this program, fread() reads incorrect values into the variables! Otherwise, it runs fine.
    The compiler is DevC++, running on Windows 98. If anyone can compile and run this program to duplicate these results, I would sincerely appreciate it.
    I would be interested to see if other compilers produce the same results as well....I need to know if this is a compiler or OS bug!!!

    Code:
    #include <iostream>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
     const char file[] = "x.dat";
    
     int a = 123;
     int b = 456;
    
     cout << "a: " << a << endl;
     cout << "b: " << b << endl << endl;
    
     FILE * fp = fopen(file, "rb");
    
       if(fp)
      {
       fread(&a, sizeof(int), 1, fp);
       fread(&b, sizeof(int), 1, fp);
       fclose(fp);
      }
       else
      {
          fp = fopen(file, "wb");
    
             if(fp)
            {
             fwrite(&a, sizeof(int), 1, fp);
             fwrite(&b, sizeof(int), 1, fp);
             fclose(fp);
            }
    
      }
    
     cout << "a: " << a << endl;
     cout << "b: " << b << endl;
    
     cin.get();
    
     return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>fread() reads incorrect values into the variables
    How do you know it read any? You aren't checking its return code.

    Can you explain the problem a bit more please.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    Your code does work, with one slight change.

    cout by itself means nothing. You'd have to either use std::cout for each time you use it, or adding using namespace std after your #includes.

    EDIT : Forgot the 'or'. Slap me.
    Using Dev-C++ beta under Win XP Pro. That or g++ on Mandrake Linux 9.0 .

  4. #4
    Frantic1
    Guest
    Let me explain a little better. I have a program that proccesses files that are dragged and dropped to it (unless argc == 1, in which case, the user can change certain settings). Suddenly, I realized that the program's configuration flags were set incorrectly, but this only happened when argc>1! Note that the program's code up to that point is the same for either mode. And yes, I was checking the return value of fread - it was succeeding! There you have it. Somehow the Windows Drag and Drop feature is causing the weird bug to occur. Needless to say, I'd better get a hold of another compiler(or OS!)!

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    drag and drop

    I don't understand how your program can be accepting files since you are not doing anything with the argc argv arguments passed to the main function. For drag and drop to work you usually have to read in these arguments so that it can determine the file name and open it.
    zMan

  6. #6
    FranticNoMore
    Guest
    Mr. Salem. You are a genius!!! I really appreciate your help. Thank you, thank you, thank you. I really owe you one. Or several. Again, thanks so much. I will be more observant in the future!

    - Neil

  7. #7
    FranticNoMore1
    Guest
    Thanks again, Salem. I inserted this into the code and now it runs fine:



    Code:
    char * PrependPath(char * filename, const char * argv0)
    {
     char temp[strlen(filename)+1];
    
     strcpy(temp, filename);
    
     strcpy(filename, argv0);
    
     char * next = filename, * end = filename;
    
     do{
    
        next = strstr(next, "\\");
    
        if(next)end = ++next;
    
        }while(next);
    
     *end = 0;
    
     strcat(filename, temp);
    
     return filename;
    }
    
    
    
    
    
    
    #include <iostream>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
     char file[10240] = "x.dat";
    
     PrependPath(file, *argv);
    
     int a = 123;
     int b = 456;
    
     cout << "a: " << a << endl;
     cout << "b: " << b << endl << endl;
    
     FILE * fp = fopen(file, "rb");
    
       if(fp)
      {
       fread(&a, sizeof(int), 1, fp);
       fread(&b, sizeof(int), 1, fp);
       fclose(fp);
      }
       else
      {
          fp = fopen(file, "wb");
    
             if(fp)
            {
             fwrite(&a, sizeof(int), 1, fp);
             fwrite(&b, sizeof(int), 1, fp);
             fclose(fp);
            }
    
      }
    
     cout << "a: " << a << endl;
     cout << "b: " << b << endl;
    
     cin.get();
    
     return 0;
    }


    I appreciate your help!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OS 'Compiler'?
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2009, 02:54 AM
  2. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  3. a simple OS
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-06-2004, 10:47 PM
  4. OpenScript2.0 Compiler
    By jverkoey in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 12:52 PM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21