File I/O Assertion Failure in VS2008

This is a discussion on File I/O Assertion Failure in VS2008 within the C Programming forums, part of the General Programming Boards category; Hello… I am currently working on a problem that requires a program that takes a command line argument that is ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164

    File I/O Assertion Failure in VS2008

    Hello…

    I am currently working on a problem that requires a program that takes a command line argument that is the name of a text file and creates a new text file with a heading line and the contents of the original file with line numbers added. If the file’s name contains a period, use the part of the name before the period concatenated with .lis as the name of the new file. Otherwise, just concatenate .lis with the while file name.

    That is the assignment as copied from the textbook. There is a sample program that is pretty close to the core of this problem, so I copied it out of the book as it was. I was going to test it then start the modification to give the output required. But, I have run into a snag. When I copy the program out of the text, I get an assertion failure. I have checked and rechecked the code to make sure I have everything exact, and I do. It compiles just fine, but at runtime I get the failure. Can anyone give me a clue as to why this isn’t working?

    I am using Visual Studio 2008. This is the exact error:
    Debug Assertion Failed!
    Program:…Assignment_7-1.exe
    File:f:\dd\vctools\crt_bld\self_x86\crt\src\fopen. c
    Line: 54

    Expression: (file != NULL)

    For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

    This is my code that caused that failure:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int
    main(int argc,
    	 char *argv[])
    {
        FILE *inp,
    		 *outp;
        char ch;
        //char name[30];
        
        //printf("\nType backup and the name of the file to copy and the new file name >");
       
        inp = fopen(argv[1], "r");
        if (inp == NULL) {
            printf("\nCannot open file %s for input\n", argv[1]);
            exit(1);
        }
        outp = fopen(argv[2], "w");
        if (outp == NULL) {
            printf("\nCannot open file %s for output\n", argv[2]);
            exit(1);
        }
    
    	//strcat(argv[2], ".lis");
        
        //   fprintf(output, "***************** %s ***************", argv[2]);
        
    /*    char line[500];
        int counter = 1; */
    
       /*fgets(line,500,input);*/
               
    /*          while(!EOF (input))  {
                   fgets(line,500,input);
                   fprintf(output, "%d : %s", counter, line);
                   counter++;
               }*/
       for (ch = getc(inp); ch != EOF; ch = getc(inp))
           putc(ch, outp);
       
       
            fclose(inp);
            fclose(outp);
            printf("\nCopied %s to %s\n", argv[1], argv[2]);
            
            return(0);
    }
    This code is directly out of the book, but when I test it before moving on with my modification to complete the rest of the assignment, it fails.

    Thanks for taking a look and offering any advice.

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    498
    I don't use VS2008 but i am guessing that you need to supply the command line parameters for this as you are using them to open files.
    Try running the exe generated(after build) with arguments from command prompt and check whether the error reappears.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    498
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Thanks for the input and references, I will check them out. If I still can't get it, I will repost.

    Thanks again...

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    The error basically says that you are trying to perform an operation on an invalid file handle (the handle is NULL).
    Cannot really say why. But you can use the debugger if you cannot get it to work with the command line arguments.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > File:f:\dd\vctools\crt_bld\self_x86\crt\src\fopen.c
    My guess is the function you called was fopen()

    > Expression: (file != NULL)
    http://msdn.microsoft.com/en-us/libr...cb(VS.80).aspx
    Given two parameters, filename and mode, to choose from, which of them could possibly be NULL when it's supposed to be non-null?

    In short, you're using argv before checking argc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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