Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,664 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,168 people online right now. Registration is fast and FREE... Join Now!




question about int conversion

 
Reply to this topicStart new topic

question about int conversion

ynnhoj67
post 15 Jul, 2007 - 10:58 AM
Post #1


New D.I.C Head

*
Joined: 13 Jul, 2007
Posts: 11


My Contributions


hi~
Im writing int number into a file, the problem is that the compiler would consider that int number as a ASCII code so it will output the character which realted to that number in the file instead the number.
is there any way to convert it, in order to make it output number in that file?
bty, im using c++. thanks.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 15 Jul, 2007 - 11:49 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


If you are writing to the file in a standard manner, there should be no problem writing an int to a file. Can you post the code you're using?

User is online!Profile CardPM

Go to the top of the page

ynnhoj67
post 15 Jul, 2007 - 12:15 PM
Post #3


New D.I.C Head

*
Joined: 13 Jul, 2007
Posts: 11


My Contributions


QUOTE(Amadeus @ 15 Jul, 2007 - 12:49 PM) *

If you are writing to the file in a standard manner, there should be no problem writing an int to a file. Can you post the code you're using?


the code is
CODE

main()
{
      int num;
      printf("Please enter a number: ");
      scanf("%d",&num);
      FILE*data;
      data=fopen("file.txt","w");
      fwrite(&num,sizeof(num),1,data);
      fclose(data);
      }

for example,if u enter 65 then in file.txt it shows up "A" which is related to its ASCII code.
User is offlineProfile CardPM

Go to the top of the page

zyruz
post 15 Jul, 2007 - 03:43 PM
Post #4


New D.I.C Head

*
Joined: 13 Aug, 2005
Posts: 31


My Contributions


You need to store the int in ASCII or open the file in a program that show files in binary.

You can use itoa to change the int to ASCII version.
like:
CODE
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
      int num;
      char buffer[33];
      printf("Please enter a number: ");
      scanf("%d",&num);
      FILE*data;
      itoa(num,buffer,10);
      data=fopen("file.txt","w");
      fwrite(&buffer,sizeof(buffer[0]),strlen(buffer),data);
      fclose(data);
}



This post has been edited by zyruz: 15 Jul, 2007 - 03:49 PM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 15 Jul, 2007 - 03:51 PM
Post #5


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


As an FYI, the code you are using is C, not C++.
User is online!Profile CardPM

Go to the top of the page

ynnhoj67
post 15 Jul, 2007 - 06:59 PM
Post #6


New D.I.C Head

*
Joined: 13 Jul, 2007
Posts: 11


My Contributions


thx zyruz...
bty, the code im using is c?
User is offlineProfile CardPM

Go to the top of the page

dogboi
post 15 Jul, 2007 - 07:49 PM
Post #7


D.I.C Head

Group Icon
Joined: 10 Jul, 2007
Posts: 93



Dream Kudos: 25
My Contributions


QUOTE(ynnhoj67 @ 15 Jul, 2007 - 09:59 PM) *

thx zyruz...
bty, the code im using is c?


Yup, it's C. In C++, you use streams to deal with files.


CODE
#include <fstream.h>
#include <iostream.h>

int main(int argc, char *argv[])
{
    ofstream outfile("file.txt"); //create ofstream object
    int num;

    cout<<"Please enter a number: ";
    cin>>num;
    outfile<<num;
}




This post has been edited by dogboi: 15 Jul, 2007 - 07:57 PM
User is offlineProfile CardPM

Go to the top of the page

ynnhoj67
post 15 Jul, 2007 - 09:54 PM
Post #8


New D.I.C Head

*
Joined: 13 Jul, 2007
Posts: 11


My Contributions


QUOTE(dogboi @ 15 Jul, 2007 - 08:49 PM) *


Yup, it's C. In C++, you use streams to deal with files.


o ic, thx. but that's how i learned in my c++ course
anyways, as long as the code works and i learned about it.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 16 Jul, 2007 - 04:05 AM
Post #9


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


Yeah, the difference between c and c++ is the reason I said there should be no problem writing an int to a file...very straightforward as noted by dogboi.
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month