Hi, i have been trying to figure out how do i read some digits from a file and store them as separate numbers in an array, so far i can read it as one integer and use it but how do i divide a line of digits for example i have '1234' and i want them to be number[0]=1, number[1]=2 ...
#include <iostream>
...
usingnamespace std;
int main ()
{
// make file and write some digits in it
ofstream makefile;
makefile.open ("test.txt");
makefile << "12";
makefile.close();
// ask to input filename and open it
char filename [60];
ifstream OpenFile;
cin.getline (filename, 60);
OpenFile.open (filename);
// if this file does not exist then end the program
if (!OpenFile.is_open())
{
exit(EXIT_FAILURE);
}
// read those digits as integers
string str;
int number[10];
OpenFile >> str;
stringstream MyStream(str);
MyStream >> number[0];
// check whether it is integer
cout << "the integer is : " << number[0] << endl;
cout << "integer + 3 equals to " << (number[0]+3) << endl;
system ("pause");
return 0;
}
However, in your case I think it may be simpler to read your file as characters, instead of integers. Just read the first char, turn it into an integer and there is your digit.
int ReadingDigits( char filename[])
{
ifstream OpenFile;
OpenFile.open (filename);
// if this file does not exist then end the program
if (!OpenFile.is_open())
{
exit(EXIT_FAILURE);
}
// read those digits as integers
char digit;
int number[1000];
int i = 0;
int nCount;
while (OpenFile.good())
{
OpenFile >> digit;
number[i]= digit - '0';
i++;
nCount = i-1;
}
OpenFile.close();
cout << "there are " << nCount << " digits in this file" << endl;
// check whether it is integer
return ?? ;
}
yes. thats correct, i do want to use that array for something :) but when i try to return the number there is an error: invalid conversion from 'int*' to 'int' as well as warning: address of local variable 'number' returned
When you return an array you're actually returning a pointer to its first element.
Using vectors is simplier and risk free. If however you insist in using arrays you need to change the return type to int*, you'll also need to know how many elements are in the array so that you can process it without going out of the array bondaries.
Have tou consider what happens if your file has more than 1000 digits?(It's not pretty =P)
If you're returning an array, you need to allocate the memory using new, and then remember to free it later. Which is why it's far better -- as eidge said - to use a vector.
The alternative is too horrible to say! (declare the array as static, so it doesn't disappear when you go out of scope). Returning a local variable will just blow up, as you no doubt know.
The old-school C-style approach to the array return problem is:
int ReadingDigits( const char* filename, char* buffer, int bufferSize )
{
...
}
In this case the return can be an error code (e.g. buffer to small)
P.S. I almost always see file name variable declared as (const) char*... I'm curious where the char[] habit comes from?
eidge , in this case i know that there won't be more than 1000 digits in the file. and i haven't faced the vectors yet so i am trying to do it this way. I am really a beginner but ready to learn :)
i am not sure whether i am using the pointers correctly here... when the program should execute this line: cout << *Point << endl; it crashes.
if somebody could tell me why and don't go too harsh on me :D
usingnamespace std;
// ReadingDigits - reads digits from a txt file and puts them into an array of integers
int ReadingDigits(char* filename, int* Address);
int main()
{
int* Point;
char filename[60];
cout << "enter the name of the file : ";
cin.getline(filename, 60);
ReadingDigits(filename, Point);
cout << *Point << endl;
system("pause");
return 0;
}
int ReadingDigits( char* filename, int* Address)
{
ifstream OpenFile;
OpenFile.open (filename);
// if this file does not exist then end the program
if (!OpenFile.is_open())
{
cout << "can't open this file!" << endl;
exit(EXIT_FAILURE);
}
// read those digits as integers
char digit;
int number[1000];
int i = 0;
int nCount;
while (OpenFile.good())
{
OpenFile >> digit;
number[i]= digit - '0';
i++;
nCount = i-1;
}
OpenFile.close();
cout << "there are " << nCount << " digits in this file" << endl;
Address = &number[0];
// check whether it's working
cout << (*Address+1) << endl;
}
usingnamespace std;
int main()
{
char filename[60];
cout << "enter the name of the file : ";
cin.getline(filename, 60);
ifstream OpenFile;
OpenFile.open (filename);
// if this file does not exist then end the program
if (!OpenFile.is_open())
{
cout << "can't open this file!" << endl;
exit(EXIT_FAILURE);
}
// read those digits as integers
char digit;
int number[1000];
int i = 0;
int nCount;
while (OpenFile.good())
{
OpenFile >> digit;
number[i]= digit - '0';
i++;
nCount = i-1;
}
OpenFile.close();
cout << "there are " << nCount << " digits in this file" << endl;
int GreatestProd = 0;
int tempProduct;
for (int t = 0; t < nCount; t++)
{
tempProduct = number[t];
// PROGRAM WORKS TILL THIS PLACEfor (int n = t; n <= (n+4); n++)
{
tempProduct *= number[n];
}
if (tempProduct > GreatestProd)
{
GreatestProd = tempProduct;
}
}
cout << "the greatest product of five consecutive" << endl;
cout << "digits in the given 1000-digit number is : " << GreatestProd << endl << endl;
system("pause");
return 0;
}