| | |
need help reading from a file.
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,757
Reputation:
Solved Threads: 491
•
•
•
•
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 |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






