reading txt file into array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 5
Reputation: LisaJane is an unknown quantity at this point 
Solved Threads: 0
LisaJane LisaJane is offline Offline
Newbie Poster

reading txt file into array

 
0
  #1
Feb 3rd, 2005
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;
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: reading txt file into array

 
0
  #2
Feb 3rd, 2005
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.
#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;  

}
Oh and please use Code Tags
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 5
Reputation: LisaJane is an unknown quantity at this point 
Solved Threads: 0
LisaJane LisaJane is offline Offline
Newbie Poster

Re: reading txt file into array

 
0
  #3
Feb 3rd, 2005
Okay so that was my stupid mistake...I actually have 30 values...so I made the array size 41.

but the changes you made worked great, and I will remember the code tags :o I didn't expect a response so helpful...really appreciate it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: reading txt file into array

 
0
  #4
Feb 3rd, 2005
Hey I am not always mean .
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: reading txt file into array

 
0
  #5
Feb 4th, 2005
Of course an array is not a hashtable...
There's a hashtable class in the STL, use that.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 5
Reputation: LisaJane is an unknown quantity at this point 
Solved Threads: 0
LisaJane LisaJane is offline Offline
Newbie Poster

Re: reading txt file into array

 
0
  #6
Feb 4th, 2005
I know...its just that I am in over my head, and figure if i start simple it might all start to make sense
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: reading txt file into array

 
0
  #7
Feb 4th, 2005
>There's a hashtable class in the STL, use that.
Not according to the standard, though a hashmap is a common extension.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC