-1

I'm trying to read a number from a text file, and I'm not allowed to use a binary file. I've tried two methods to do this, and both return a strange result.

The first method:

    char *theNumber; 
    int i = 0;
    while(data>>text)
    {
        theNumber[i] = text;  
        i++;
    }

returns some weird accented characters. The second

int theNumber;
while(data>>text)
{
     theNumber = text; // I tried theNumber<<text; as well
}

When I cout the result of this one it returns some big number when the text file contained 123.


string filename;
    char text;
    int p; //first prime number
    int q; //second prime number
    unsigned long long toBeEncrypted; 

    cout<<"Enter name of file to encrypt: ";
    cin>>filename;
    ifstream data;
    ofstream encryptedData;
    encryptedData.open("RSA_cipher.txt");


    cout<<"Please enter two prime numbers:"<<endl;
    p = getPrime(1);
    q = getPrime(2);

    //doing stuff with file
    int theNumber;
    data >> theNumber;
    //int i = 0;
    /*while(data>>text)
    {
        theNumber[i] = text;  
        i++;
    }*/cout<<theNumber;

...//other stuff unrelated to the problem

SemicolonExpected
  • 614
  • 2
  • 12
  • 37

3 Answers3

1

This code:

char *theNumber; 
int i = 0;
while(data>>text)
{
    theNumber[i] = text;  
    i++;
}

Has Undefined Behavior, because you are using theNumber[i] to access an array which you haven't even allocated. You should have done:

 char theNumber[255]; // Buffer size depends on the particular application
 int i = 0;
 while(data>>text)
 {
     theNumber[i] = text;  
     i++;
}

The second attempt:

theNumber = text;

May or may not work, depending on how you defined text. This is impossible to answer without knowing the definition of text.

Anyway, if you want to read in a number from an input stream, just do:

int number;
data >> number;

UPDATE:

In the last code snippet you updated, the data stream is constructed, but never open. It is not associated to any file. Therefore, attempting to read from that stream won't succeed, and nothing will be stored into number (which is uninitialized).

ifstream data;

// data is not associated to any file after construction...

int theNumber;
data >> theNumber;
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • I mistyped that, I meant that with 1 = sign. That code wasn't c/p'd on and I manually typed it in sorry. I tried doing your method with `data>>number` and it still prints out a long number – SemicolonExpected Mar 07 '13 at 21:33
  • 2
    @ArcaneExplosion: Prints out when? How do you store the number? Are you still using `text` and `theNumber`? What is the definition of `text`? There are a lot of things I can't see from your question's text. Try to put there a short, complete example of the code you are using. Otherwise, I'll be solving problems you don't have and not solving those you have – Andy Prowl Mar 07 '13 at 21:35
  • No I'm not using the whole while loop and I just do `data >> theNumber` and I print it out immediately after for error checking purposes. Also in the second example `theNumber` is an `int` – SemicolonExpected Mar 07 '13 at 21:36
  • @ArcaneExplosion: So what is the content of the file you're reading from? – Andy Prowl Mar 07 '13 at 21:37
  • Just a simple number. "123" as I stated in the above. – SemicolonExpected Mar 07 '13 at 21:38
  • I couldn't c/p my entire program, but I could put in the relevant function where the problem is occuring. All the variables used are local to that function. – SemicolonExpected Mar 07 '13 at 21:43
  • OH MY GOD, how did I overlook this THANK YOU SO MUCH – SemicolonExpected Mar 07 '13 at 22:01
  • @ArcaneExplosion: That happens ;-) – Andy Prowl Mar 07 '13 at 22:02
0

This does not create storage for your number.

char *theNumber; 

It's a pointer. It points somewhere arbitrary, since you haven't assigned an address to it.

Try this.

char theNumber[10];  // Whatever size you need. 

Or this.

int theNumber; 
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

You didn't allocate any memory for char *theNumber;.
The theNumber points to a random location and you are printing random characters