954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

reading txt file into array

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 :confused: ), 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
#include
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;
}

LisaJane
Newbie Poster
5 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

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

prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 

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.

LisaJane
Newbie Poster
5 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

Hey I am not always mean ;).
p.s. Your welcome.

prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 

Of course an array is not a hashtable...
There's a hashtable class in the STL, use that.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

I know...its just that I am in over my head, and figure if i start simple it might all start to make sense :)

LisaJane
Newbie Poster
5 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

>There's a hashtable class in the STL, use that.
Not according to the standard, though a hashmap is a common extension.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Okay i had a question, how would you do this same exact thing except with a multidimensional array, and if possible convert that array in integers, i can hand the converting, i was having more problems with the multidimensional aspect(I am a student and i have been looking for examples for this, i will post code if you want me to)

mark4444
Newbie Poster
1 post since Apr 2010
Reputation Points: 8
Solved Threads: 0
 
Okay i had a question, how would you do this same exact thing except with a multidimensional array, and if possible convert that array in integers, i can hand the converting, i was having more problems with the multidimensional aspect(I am a student and i have been looking for examples for this, i will post code if you want me to)

Please create new thread and any supporting coding. Re-opening years old thread is not good idea to get reply from members.

Thread closed

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You