Thread: File I/O stopped working.

  1. #1
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25

    File I/O stopped working.

    Strange occurence when working with file input output:

    Here is my code.
    Code:
    #include <stdio.h>
    int main()
    {
    FILE *fp;
    fp=fopen("c:\\test.txt", "wb");
    char x[10]="ABCDEFGHIJ";
    fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fp);
    system("Pause");
    }

    Here is what she looks like when compiling.

    Code:
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    poop.c
    poop.c(7) : error C2143: syntax error : missing ';' before 'type'
    poop.c(8) : error C2065: 'x' : undeclared identifier
    poop.c(8) : warning C4022: 'fwrite' : pointer mismatch for actual parameter 1
    poop.c(8) : error C2065: 'x' : undeclared identifier
    poop.c(8) : error C2109: subscript requires array or pointer type
    poop.c(8) : error C2065: 'x' : undeclared identifier
    poop.c(8) : error C2065: 'x' : undeclared identifier
    poop.c(8) : error C2109: subscript requires array or pointer type
    poop.c(8) : warning C4047: 'function' : 'size_t' differs in levels of indirectio
    n from 'FILE *'
    poop.c(8) : warning C4024: 'fwrite' : different types for formal and actual para
    meter 2
    poop.c(8) : error C2198: 'fwrite' : too few arguments for call

    This was compiling and working previously. I am unsure of what happened and why. Thank you for your input on my output

    Note: test.txt is located at C:\test.txt

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Compiles and runs fine for me in GCC (although I've included 2 warnings below you should be aware of: C90 says you should declare all your varables before doing anything else, and you should explicitly return a value from main). So it's not a code problem - must be something to do with your environment, compiler invocation or configuration or something.

    test.c:6:1: warning: ISO C90 forbids mixed declarations and code [-pedantic]
    test.c:8:1: warning: control reaches end of non-void function [-Wreturn-type]

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    char x[10]="ABCDEFGHIJ";
    This likely isn't related to your problem, but you're not leaving room for the null character here.

    [edit] Actually, I don't think that's strictly necessary if you're simply using it as a character array and not a string.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Why you opened new thread?

    Selection Sort

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Matticus View Post
    Code:
    char x[10]="ABCDEFGHIJ";
    This likely isn't related to your problem, but you're not leaving room for the null character here.
    In this case what will happen?The compiler cuts off the last letter or does not store the null terminator?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by std10093 View Post
    In this case what will happen?The compiler cuts off the last letter or does not store the null terminator?
    I believe it was laserlight who pointed this out to me - I'm pretty sure it just doesn't not store the null character.

    Proof:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char charArray[10] = "ABCDEFGHIJ";
        int i;
    
        for(i=0; i<10; i++)
            printf("%c\n",charArray[i]);
    
        return 0;
    }
    My apologies for going off topic, I'll stop here.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Yes it is a problem, and should actually be caught by the compiler. The end of string character will be placed at the end of the string, which in this case will be past the boundary of the array.

    Jim

  8. #8
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    - You need to include the stdlib.h header file in order to gain access to the system function.
    - You ought to check the return value of fopen to ensure that the stream was opened successfully.
    - You need to get a compiler that supports mixed declarations and code, such as GCC or Clang.

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by jimblumberg View Post
    Yes it is a problem, and should actually be caught by the compiler. The end of string character will be placed at the end of the string, which in this case will be past the boundary of the array.

    Jim
    As Matticus said, C will not add a null terminator in that case. It's basically a non-null terminated array of char containing "ABCDEFGHIJ". It's a problem only if the code expects the null terminator to be there.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Thank you, christop. To verify, I found this in the "current draft of the C standard, containing C99" (at this link)

    6.7.8 Initialization

    32 EXAMPLE 8 The declaration

    Code:
    char s[] = "abc", t[3] = "abc";
    defines "plain" char array objects s and t whose elements are initialized with character string literals.
    This declaration is identical to

    Code:
    char s[] = { 'a', 'b', 'c', '\0' },
         t[] = { 'a', 'b', 'c' };
    The contents of the arrays are modifiable.

  11. #11
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25
    Solved:

    Something had to have been incorrect with my permissions on C: drive.

    Tried switching to a desktop file which still returned failure.

    This is the correct way to write code to files in C:
    Code:
    C:\\users\\hello\\desktop\\text.txt
    // is the correct way to write /'s in file locations for all others who run into problems with file input output

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ****.exe stopped working (Second project i have)
    By asediugeneral in forum C Programming
    Replies: 3
    Last Post: 09-03-2012, 01:25 PM
  2. Program has stopped working...
    By asediugeneral in forum C Programming
    Replies: 4
    Last Post: 09-03-2012, 11:42 AM
  3. ****.exe has stopped working
    By kawaikx15 in forum C Programming
    Replies: 10
    Last Post: 11-19-2011, 07:38 AM
  4. .exe has stopped working
    By bluesky16 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2011, 12:58 PM
  5. vshost.exe has stopped working
    By Marty21 in forum C# Programming
    Replies: 1
    Last Post: 06-08-2009, 10:41 AM