| | |
reading txt file into array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2005
Posts: 5
Reputation:
Solved Threads: 0
Hello,
I have an assignment where I have to create a hash table and load 30 part numbers into it- should be a 2-d array...and eventually I have to prompt the user to choose an algorithm and keep track of collisions. But I am starting really small with this and want to start with reading a text file into the array. I am close (i think
), but I think I may have put the values into my txt file incorrectly. Can anyone offer advice. I will show my code also
the text file just has the numbers 101-110, 301-310 & 501-510 in a list.
//read part numbers from txt file 'parts.txt'
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int i;
int array[41];
for(i=0; i<5; i++) //clear array
array[i] = 0;
ifstream in("part.txt", ios::in | ios::binary);
if(!in){
cout << "cannot open file.\n";
return 1;
}
in.read((char *) &array, sizeof array); //read block of data
for(i=0; i<5; i++) //show values read from file
cout << array[i] << " ";
in.close();
return 0;
}
I have an assignment where I have to create a hash table and load 30 part numbers into it- should be a 2-d array...and eventually I have to prompt the user to choose an algorithm and keep track of collisions. But I am starting really small with this and want to start with reading a text file into the array. I am close (i think
), but I think I may have put the values into my txt file incorrectly. Can anyone offer advice. I will show my code alsothe text file just has the numbers 101-110, 301-310 & 501-510 in a list.
//read part numbers from txt file 'parts.txt'
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int i;
int array[41];
for(i=0; i<5; i++) //clear array
array[i] = 0;
ifstream in("part.txt", ios::in | ios::binary);
if(!in){
cout << "cannot open file.\n";
return 1;
}
in.read((char *) &array, sizeof array); //read block of data
for(i=0; i<5; i++) //show values read from file
cout << array[i] << " ";
in.close();
return 0;
}
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
Well for one you declare a array with 41 elements and only seem to need 5. And the lovely thing about fstream is operator overloading so if you want to read in something it's very easy.
Oh and please use Code Tags
#include <iostream> #include <fstream> int main() { int i = 0; int array[5]; int max_read = 5; int amountRead = 0; std::ifstream in("part.txt",std::ios::in |std::ios::binary); if(!in) { std::cout<<"Could not open file"<<std::endl; return 1; } //this is where we are reading in the information into our array while(in>>array[amountRead]&& amountRead < max_read) { amountRead++; } for(i = 0; i < 5; i++) { std::cout<<array[i]<<std::endl; } std::cin.get(); return 0; }
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
Hey I am not always mean
.
p.s. Your welcome.
. p.s. Your welcome.
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
![]() |
Similar Threads
- Reading a list from txt file to array... (Pascal and Delphi)
- Read number in txt file into an Array (C++)
- reading a txt file into a 2d array (C++)
Other Threads in the C++ Forum
- Previous Thread: Double values with two decimal places
- Next Thread: I just need help starting this program please
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






