| | |
need help reading from a file.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
hi,
I am curious, how can I read a int from a text file( ex. num.txt).
but this text file contains numbers without spacing
(ex.1212132132313...
23156897984969..
583852935792...)
The problem is that this file has no spaces between numbers and when i try to read it into an arry, the result is zero because it cant hold such huge --over 1000 integer.
here is my code :
I am curious, how can I read a int from a text file( ex. num.txt).
but this text file contains numbers without spacing
(ex.1212132132313...
23156897984969..
583852935792...)
The problem is that this file has no spaces between numbers and when i try to read it into an arry, the result is zero because it cant hold such huge --over 1000 integer.
here is my code :
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> using namespace std; int main(){ ifstream num("num.txt"); __int64 arry[25000]={0}; for(int i=0; (i<2500 && (!num.eof())) ; i++) { num >> arry[i]; } return 0; }
do you want all the numbers together? or only 1 at a time and get it into an array...
Here's what i did
get it as a string
Here's what i did
get it as a string
C++ Syntax (Toggle Plain Text)
char line[2500]; ifstream num("num.txt"); num.getline(line,2500); // get the line num.close(); int array[2500]; for(int i = 0; i < (int)strlen(line); i++) array[i] = (int)line[i]-48; // -48 because '0' = 48, '1' = 49, it gives normal int...
Last edited by u8sand; Dec 31st, 2008 at 12:22 pm.
Or just:
If you're parsing a string, then possibly substring it and use istringstream to place it into an int.
You'd probably also have to check the string size(counting till non-num found), and compare it to string.max_size().
C++ Syntax (Toggle Plain Text)
ifstream file("name"); sting str; if(file.is_open()) { file >> str; file.close(); }
If you're parsing a string, then possibly substring it and use istringstream to place it into an int.
You'd probably also have to check the string size(counting till non-num found), and compare it to string.max_size().
Last edited by MosaicFuneral; Jan 1st, 2009 at 12:18 am.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
•
•
do you want all the numbers together? or only 1 at a time and get it into an array...
Here's what i did
get it as a string
C++ Syntax (Toggle Plain Text)
char line[2500]; ifstream num("num.txt"); num.getline(line,2500); // get the line num.close(); int array[2500]; for(int i = 0; i < (int)strlen(line); i++) array[i] = (int)line[i]-48; // -48 because '0' = 48, '1' = 49, it gives normal int...
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
I get it, but it seems that your code gets only a line. Where as I need about a hundred of line worth of numbers. Ant clue on how to tell the compiler that if it reaches a number < 0 , then skip it go to new line?
•
•
•
•
tell the compiler that if it reaches a number < 0 , then skip it go to new line
get . Use isdigit from the cctype library to check if the character is a digit. If it is, convert the character to an integer and add it to the array. If not, throw the character away. I'm not 100% sure this is what you want. ![]() |
Similar Threads
- Reading from file, passing into function. (C)
- Help on a reading from a file programming (Java)
- Need help reading a file (C++)
- Reading a file into a Parallel Array (C++)
- problems with reading in file (C++)
- First year assigment on reading file, sorting and outputting invoice (C++)
- Error Message Concerning Reading File From A Drive (C++)
- reading a file into code (Java)
Other Threads in the C++ Forum
- Previous Thread: variables scope
- Next Thread: simple visual studio express problem
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class code coding compile console conversion count data database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






