Read From File Text and Store in a Map

  • Forums
  • Bits & Bytes
  • Webmastering & Programming
You are using an out of date browser. It may not brandish this or other websites correctly.
Y'all should upgrade or employ an alternative browser.

c++ storing a text file of numbers in an array

  • Thread starter MadJuggla9
  • Start date
  • #1
Joined
Oct 9, 2002
Messages
three,515
i made a text file with some numbers in information technology *sperated past a return* i desire to open up the file and store the numbers in an array. and then i tin can recall whatsoever number is in the position i want. im new to c++ so have it piece of cake, heres what i have so far
                                                              ifstream fsnumbers;     fsnumbers.open ("numbers.dat");      int array[35];             int counter = 0;     while (counter < 34)     {         fsnumbers >> array[counter];         cout << fsnumbers;         cout << endl;         counter++;     }                            

im lost on how to get stuff from a text file read and printed.
  • #2
Joined
Nov 4, 2002
Messages
4,767
Go ahead and attempt this:
                              const int MAX_NUMBERS = 34; int array[MAX_NUMBERS];  ifstream fsnumbers; fsnumbers.open("numbers.dat");         for (int i = 0; i < MAX_NUMBERS; i++) {     // decrease 48 for ascii to int conversion     array[i] = fsnumbers.go() - 48; }                            

The reason you lot subtract 48 is considering the .become() part returns the ascii integer code for any graphic symbol is next in fsnumbers. As you can see from an ascii table, integers showtime at number 48. The only problem with this method is you really don't know how many characters y'all have inputted.
  • #3
Joined
October 9, 2002
Messages
3,515
Go ahead and try this:
                                  const int MAX_NUMBERS = 34; int array[MAX_NUMBERS];  ifstream fsnumbers; fsnumbers.open("numbers.dat");         for (int i = 0; i < MAX_NUMBERS; i++) {     // decrease 48 for ascii to int conversion     array[i] = fsnumbers.get() - 48; }                                

The reason you subtract 48 is considering the .get() role returns the ascii integer code for whatsoever character is side by side in fsnumbers. Equally you can meet from an ascii table, integers commencement at number 48. The only trouble with this method is you really don't know how many characters yous have inputted.

characters? ill have 35 lines composing of the numbers 1-35. ill increase it later mayhap
  • #four
Joined
Jun 13, 2003
Messages
i,670
Here's another case using a vector instead, so you lot don't have to worry virtually arrays and sizes. Each chemical element in the vector is a string. (assuming you only demand to display the numbers. The loop at the stop was non needed to impress out each line. Information technology's there for example purposes.).
                              #include <iostream> #include <cord> #include <vector> #include <cstdlib> #include <fstream>  using namespace std;  int main() {     const string file = "file.txt";     ifstream in(file.c_str());     if (!in) {         cout << endl << "Fault reading " << file << endl;         exit(EXIT_FAILURE);     }     vector<cord> nums;     for (string s; getline(in,s); ) {         nums.push_back(southward);     }     cout << endl;     for (size_t i = 0; i < nums.size(); i++) {         cout << nums.at(i) << endl;     } }                            

Hither'southward the same thing merely of int type so you lot can perform operations between elements and specify what file to read via a command line parameter.
                              #include <iostream> #include <cord> #include <vector> #include <cstdlib> #include <fstream> #include <sstream>  using namespace std;  int toInt(const cord& s) {     stringstream convert;     catechumen << s;     int i;     catechumen >> i;     return i; }  int main(int argc, char *argv[]) {     if (argc == 2) {         const cord file = argv[1];         ifstream in(file.c_str());         if (!in) {             cout << endl << "Mistake reading " << file << endl;             get out(EXIT_FAILURE);         }         vector<int> nums;         for (cord southward; getline(in,s); ) {             nums.push_back( toInt(south) );         }         cout << endl;         for (size_t i = 0; i < nums.size(); i++) {             cout << nums.at(i) << endl;         }         } else {         cout << endl << "Usage: file.exe filename" << endl;     } }                            
  • #five
Joined
Nov four, 2002
Letters
4,767
characters? sick have 35 lines composing of the numbers 1-35. ill increase it later maybe
Oh, I see. This is quick and muddied only it should work. At that place is a LOT this won't check for similar making sure your input doesn't exceed the maximum size of an integer.
                              const int MAX_NUMBERS = 35; // # of lines const int MAX_NUM_SIZE = 12; // line size int array[MAX_NUMBERS]; char in[MAX_NUM_SIZE];  ifstream fsnumbers; fsnumbers.open("numbers.dat"); 	        for (int i = 0; i < MAX_NUMBERS; i++) {     fsnumbers >> in;     array[i] = atoi(in); }                            
  • #half dozen
Joined
Mar 9, 2004
Messages
3,318
Y'all have washed everything alright, in the while loop y'all read a int from the file and stored it in array[counter]. But so you endeavor to print out an fstream when your wanting to only impress out the number you but read. Supplant "cout << fsnumbers;" with "cout << array[counter];"
                                                              ifstream fsnumbers;     fsnumbers.open ("numbers.dat");      int array[35];             int counter = 0;     while (counter < 34)     {         fsnumbers >> assortment[counter];         cout << fsnumbers;         cout << endl;         counter++;     }                            
  • #7
Joined
December 1, 2004
Messages
1
That's still printing the fstream. This:
                              while (counter < 34)     {         fsnumbers >> array[counter];         cout << array[counter] << endl;         counter++;     }                            

would print the number you lot just read from the file.

----

You could fifty-fifty shorten it to:

                              while (counter < 34)     {         fsnumbers >> array[counter++];         cout << array[counter - i] << endl;     }                            

and yet non affect Big-Oh.
  • #8
Joined
Mar 9, 2004
Letters
3,318
That'south even so press the fstream
Thats cause I just quoted his code for a quick reference of what I was talking about... lol
  • #9
Joined
Oct 9, 2002
Messages
three,515
my friend told me i had a picky compiler and helped me end information technology with some help from you guys. works great :D

cheers

  • Forums
  • $.25 & Bytes
  • Webmastering & Programming

riverafiresom1957.blogspot.com

Source: https://hardforum.com/threads/c-storing-a-text-file-of-numbers-in-an-array.839440/

0 Response to "Read From File Text and Store in a Map"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel