How To Write an Int to file?

This is a discussion on How To Write an Int to file? within the C Programming forums, part of the General Programming Boards category; This probably sounds like a dumb question, but I am just trying to write an integer to an output buffer, ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    20

    How To Write an Int to file?

    This probably sounds like a dumb question, but I am just trying to write an integer to an output buffer, then write the output buffer to a file:

    /* Declaratives */

    char rptbuf[197];


    input_cnt_msg[] = "Total Input Records Read .......... ";

    int input_rec_cnt = 0;
    ;
    int main()
    {
    FILE *reportPtr;
    /* ...... input_rec_cnt is updated in program ..... */
    rptbuf[0] = '\n';
    z = 1;
    for (j=0; j<34; j++, z++) {
    rptbuf[z] = input_cnt_msg[j];}
    rptbuf[z] = input_rec_cnt;
    fputs(rptbuf, reportPtr);
    }

    I know this isn't right. I have tried several ways .... even moving the integer to a string, then moving that one byte at a time to the output buffer. Nothing works.

    Any suggestions? Sorry if this is a stupid question, but I'm new to this language and can't find it in the book.

    Thanks,
    muffin

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    23
    eeewwww,
    global variables...

    firstly, it doesnt look like you open the file. You need to use something like:

    ------------------------------------------
    reportPtr = fopen("filename", "w");
    ------------------------------------------

    this will open a file for writing. (thats what the "w" means).. it will re-write over an existing file if its there.. or it will create a new file.

    -------------------------------------------------------
    input_cnt_msg[] = "Total Input Records Read .......... ";
    -------------------------------------------------------

    ^^there needs to be a char b4 this.

    -----------------------------------------
    rptbuf[z] = input_rec_cnt;
    ------------------------------------------
    ^^ this is assigning a char and in value..
    bad.

    if you want to output ints to a file, you could convert them to a string (long way)
    or you can use something like:

    ------------------------------------------
    fprintf(reportPtr,"%d\n", intvalue);
    ------------------------------------------

    this will output the string to the file pointed to by reportPtr.

    Hope thats helped!
    ActionMan

    "THE DAY IS MYNE!!!!
    I'll take famouse titties for $400"
    -Sean Connery, Saturday Night Live

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    You can't read an integer into a string without converting it first. Play with "itoa()" to make what you wrote work.

    For what your doing, though, why not print the integer directly to file using fprintf(). This also eliminates the need for buffer "rptbuf".

    fprintf(reportPtr, "%s", input_cnt_msg);
    fprintf(reportPtr, "%i", input_rec_cnt);

    And are you opening and closing the file properly?
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    20
    Thank you both for your replies .... I only posted a part of my logic, so as not to confuse the question, but I ended up doing the opposite, because it caused both of you to question what I didn't show in my post.

    I had opened the file, and the input_rec_cnt is just the field name I used, that value is not being entered by the user, so those were not the problem.

    Anyway, I did change the code to not use a buffer space anymore, and I used the fprintf command that both of you recommended, and it works beautifully now!

    Thanks again,
    muffin

  5. #5
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    I am just trying to write an integer to an output buffer, then write the output buffer to a file
    Just by looking at these words of yours, I'll give you an example of how to do it.
    Code:
    #include<stdio.h>
    
    int main()
    {
    	//integer number
    	int i_number = 20;
    	//output buffer
    	char s_buffer[10];
    	//file pointer initialized to NULL (a special area in memory)
    	FILE *fptr = NULL;
    
    	//sprintf mean print to a string
    	sprintf(s_buffer, "%d", i_number);
    	//display the string on screen (stdout)
    	printf("%s",s_buffer);
    
    	//open a file 
    	fptr = fopen("c:\\file","w");
    	//write buffer to file
    	fprintf(fptr,"%s ",s_buffer);
    
    	return 0;
    }
    Now I'm not saying that this is necessarily the best way to go about writing your program. I don't know what your program should do, but this does what you asked for.
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  3. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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