C Code Read Integer Array From File

  • Forums
  • Bits & Bytes
  • Webmastering & Programming
Yous are using an out of date browser. It may non display this or other websites correctly.
Yous should upgrade or use an alternative browser.

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

  • Thread starter MadJuggla9
  • Start date
  • #ane
Joined
Oct 9, 2002
Messages
3,515
i fabricated a text file with some numbers in it *sperated by a return* i want to open the file and store the numbers in an array. so i tin can phone call up whatever number is in the position i want. im new to c++ so accept it easy, heres what i have so far
                                                              ifstream fsnumbers;     fsnumbers.open ("numbers.dat");      int assortment[35];             int counter = 0;     while (counter < 34)     {         fsnumbers >> assortment[counter];         cout << fsnumbers;         cout << endl;         counter++;     }                            

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

The reason yous subtract 48 is considering the .get() function returns the ascii integer code for whatever character is next in fsnumbers. As yous can run across from an ascii table, integers outset at number 48. The but problem with this method is yous really don't know how many characters y'all have inputted.
  • #3
Joined
October 9, 2002
Messages
3,515
Go alee and try this:
                                  const int MAX_NUMBERS = 34; int array[MAX_NUMBERS];  ifstream fsnumbers; fsnumbers.open up("numbers.dat");         for (int i = 0; i < MAX_NUMBERS; i++) {     // subtract 48 for ascii to int conversion     array[i] = fsnumbers.get() - 48; }                                

The reason you subtract 48 is because the .get() part returns the ascii integer code for any character is next in fsnumbers. As you can see from an ascii table, integers commencement at number 48. The simply problem with this method is you really don't know how many characters y'all have inputted.

characters? ill have 35 lines composing of the numbers 1-35. sick increase information technology later perhaps
  • #4
Joined
Jun thirteen, 2003
Letters
1,670
Here'south another example using a vector instead, so you don't have to worry about arrays and sizes. Each chemical element in the vector is a string. (bold you lot just need to display the numbers. The loop at the end was not needed to print out each line. Information technology's there for example purposes.).
                              #include <iostream> #include <string> #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 << "Mistake reading " << file << endl;         go out(EXIT_FAILURE);     }     vector<string> nums;     for (string south; getline(in,southward); ) {         nums.push_back(south);     }     cout << endl;     for (size_t i = 0; i < nums.size(); i++) {         cout << nums.at(i) << endl;     } }                            

Here's the same affair just of int blazon so you can perform operations between elements and specify what file to read via a command line parameter.
                              #include <iostream> #include <string> #include <vector> #include <cstdlib> #include <fstream> #include <sstream>  using namespace std;  int toInt(const string& due south) {     stringstream catechumen;     convert << due south;     int i;     catechumen >> i;     render i; }  int primary(int argc, char *argv[]) {     if (argc == ii) {         const string file = argv[one];         ifstream in(file.c_str());         if (!in) {             cout << endl << "Mistake reading " << file << endl;             exit(EXIT_FAILURE);         }         vector<int> nums;         for (string south; getline(in,southward); ) {             nums.push_back( toInt(southward) );         }         cout << endl;         for (size_t i = 0; i < nums.size(); i++) {             cout << nums.at(i) << endl;         }         } else {         cout << endl << "Usage: file.exe filename" << endl;     } }                            
  • #5
Joined
Nov 4, 2002
Messages
four,768
characters? ill take 35 lines composing of the numbers i-35. ill increase information technology afterwards maybe
Oh, I encounter. This is quick and dirty but it should piece of work. There is a LOT this won't check for like 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 assortment[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); }                            
  • #6
Joined
Mar 9, 2004
Messages
3,318
You lot accept washed everything alright, in the while loop you lot read a int from the file and stored it in array[counter]. But so yous endeavor to print out an fstream when your wanting to just print out the number y'all just read. Supervene upon "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++;     }                            
  • #vii
Joined
Dec 1, 2004
Letters
one
That's nonetheless printing the fstream. This:
                              while (counter < 34)     {         fsnumbers >> array[counter];         cout << array[counter] << endl;         counter++;     }                            

would print the number you just read from the file.

----

You could fifty-fifty shorten information technology to:

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

and nevertheless non affect Large-Oh.
  • #viii
Joined
Mar 9, 2004
Letters
iii,318
That's however printing the fstream
Thats cause I just quoted his lawmaking for a quick reference of what I was talking nearly... lol
  • #9
Joined
October ix, 2002
Letters
3,515
my friend told me i had a picky compiler and helped me end it with some assistance from you guys. works bang-up :D

cheers

  • Forums
  • Bits & Bytes
  • Webmastering & Programming

gerdeswhispooke.blogspot.com

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

0 Response to "C Code Read Integer Array From File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel