writing number 10 to files

This is a discussion on writing number 10 to files within the C Programming forums, part of the General Programming Boards category; i am pretty new to c programming, i have been playing with writing to files, just for messing around, i ...

  1. #1
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49

    writing number 10 to files

    i am pretty new to c programming, i have been playing with writing to files, just for messing around, i wrote a program to write 3 "points" to a file. everything works, except for the number 10, when it writes the number 10, it writes 0D 0A, instead of just 0A, it then screws up the read order or something too.

    Code:
    #include <stdio.h> 
    
    
    void main()
    {
         int counter = 0;
         FILE *filevariable;
         int temp;
    
         
         filevariable = fopen("testfile", "w");
         
         printf("enter x ; y ; z \n");
         printf("[[max is signed 32 bit]]\n");
         //have the user type "exit" to leave
          while(temp != 1)
    {
          counter++;
          fseek(filevariable,12*counter-8,SEEK_SET);
          printf(" POINT # %d\n", counter);
         scanf("%d", &temp);
         if(temp == 10) printf("10 entered");
         fwrite(&temp, 4, 1, filevariable);
         scanf("%d", &temp);
         fwrite(&temp, 4, 1, filevariable);
         scanf("%d", &temp);
         fwrite(&temp, 4, 1, filevariable);
         printf("\n\n");
         temp = counter*3;
         fseek(filevariable,0,SEEK_SET);
         fwrite(&temp, 4, 1, filevariable);
         printf("points saved!\n\n");
         }
         fclose(filevariable);
         
    
    }

  2. #2
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Before I start delving deep within logical mess, there are some things that you should fix:

    >void main()
    In C Programming we use:
    int main( void )
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    >???
    Always return 0 after being complete with main!

    Here's what you shall be using from now on for every C Program you create:
    Code:
    #include <stdio.h>
    
    int main( void )
    {
        puts( "Hello world" );
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    you can use fprintf to write the numbers to the file

    Code:
     
     fprintf(stream,"%d %d %d",num1,num2,num3);
    try that to put the numbers to the file..
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > except for the number 10, when it writes the number 10, it writes 0D 0A, instead of just 0A
    You need to open the file in binary mode - "wb", not "w"
    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.

  5. #5
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    wow thanks a bunch everyone!

    i love being a newbie, problems when u are still a newb at a certain programming language are always solved so easily.

    thanks again,
    variable

    thought id repost the finished code for all to see.

    Code:
    #include <stdio.h> 
    
    int main(void)
    {
         int counter = 0;
         FILE *filevariable;
         int temp;
         
         filevariable = fopen("testfile", "wb");
         
         printf("enter x ; y ; z \n");
         printf("[[max is signed 32 bit]]\n");
         //have the user type "exit" to leave
          while(temp != 1)
    {
          counter++;
          fseek(filevariable,12*counter-8,SEEK_SET);
          printf(" POINT # %d\n", counter);
         scanf("%d", &temp);
         fwrite(&temp, 4, 1, filevariable);
         scanf("%d", &temp);
         fwrite(&temp, 4, 1, filevariable);
         scanf("%d", &temp);
         fwrite(&temp, 4, 1, filevariable);
         printf("\n\n");
         temp = counter*3;
         fseek(filevariable,0,SEEK_SET);
         fwrite(&temp, 4, 1, filevariable);
         printf("points saved!\n\n");
         }
         fclose(filevariable);
         
         return 0;
         
    
    }

    ++ just out of curiosity, why would every number but 10 work?
    Last edited by variable; 01-30-2005 at 11:49 AM.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by variable
    ++ just out of curiosity, why would every number but 10 work?
    It happens to be a value that gets translated in text mode.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    ah thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 01:48 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Writing Bitmap Files
    By HappyDude in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2001, 05:48 PM
  5. number of open files....
    By Prakash in forum C Programming
    Replies: 2
    Last Post: 09-18-2001, 11:32 AM

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