How can I read this line of numbers, assigning each to a different variable.
1 3 4 7 2 3 5 1 2 4 1 1
I need to account for the possability of some of the ints being double or tripple digit.
Printable View
How can I read this line of numbers, assigning each to a different variable.
1 3 4 7 2 3 5 1 2 4 1 1
I need to account for the possability of some of the ints being double or tripple digit.
Im sorry, but u sound liek a newbie. Maybe you should get a book, or read some tutorials, or I could teach you if u want. Its not hard.
SPH
well, ive been codeing about a year and a half, but I am self taught, so some things ive just never had to deal with. One of those is file I/O, so when it comes to that I am a newbie.
I thought my question was rather simple and straitforward?
file io is much as any other io, if you do not understand hwo to do thisfrom a string, you will most likely not understand how it is being done from a file, for not understanding your own code is the worst thing of all. so I suggest you practise doing this with strings (by makking a class with a streampos to read along) and then I will give you some file io code using fstreams.
SPH
I found this code some time back, I understand it, but it only demonstrates reading one value at a time.
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
int main()
{
int array[10] = {1,7,3,6,2,6,6,2,8,1};
//creating output stream object to be used when saving
ofstream output;
//opening file to write to
output.open("file.txt");
int i;
//looping through all elements of the array and writing everything to the file
for (i = 0; i < 10; i++)
{
output << array[i] << endl;
}
//closing the file
output.close();
//declare array to hold data
int array2[10];
//reset i to zero;
i = 0;
//declare ifstream, associate with file, and open file
ifstream fin("file.txt");
//check if opening file not successful
if(!fin)
{
cout << "couldn't open file for reading." << endl;
}
//read in one int at a time into array2
while(fin)
{
fin >> array2[i++];
}
//display array2 to show that read in was successful
for(i = 0; i < 10; i++)
{
cout << array2[i] << endl;
}
return 0;
}
wow... that looks lust like the example that i always use...
well, here's how i do it.. it's probably the same as the above, but jsut incase something was missed.
you can use such functions as atof() and read in the numbers as strings and convert them to the appropriate format...Code:
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
int main()
{
//creating input stream to read from file into program
ifstream input;
//creating output stream to write to a file from the program
//opening file to read into the program using the previously created input stream.
input.open("test.txt");
//reading in the variables from the file and saving them into an array within the program
cout << "how many different numbers are in the file?" << endl;
int num_lines;
cin >> num_lines;
int array[num_lines];
for (int i = 0; i < num_lines; i++)
{
cin >> array[i];
}
//whether to print the array to another file or to the screen
char choice;
do
{
cout << "print to [f]ile or print to [s]creen?" << endl;
cin >> choice;
}while (choice != 'f' && choice != 's');
//printing the array to a file
if (choice == 'f')
{
//creates an output stream to write to a file with
ofstream output;
//opens a file to write to using the output stream
output.open("ofile.txt");
//cycles through the array and prints it to file
for (i = 0; i < num_lines; i++)
{
output << array[i];
}
}
//printing the array to the screen
if (choice == 's')
{
//cycles through the array and print it to the screen
for (i = 0; i < num_lines; i++)
{
cout << array[i];
}
}
}
You cant do
int array[num_lines];
can you?
I though you had to declare all array sizes at compile time, so the program will know how much memory to allocate, here you are declareing an array, and useing a variable to set the size. I tried something along that line a while back, and it never would work.
You do, but some compilers (Dev C++ is one) will let you do what Aran has, but as it's highly non-standard, it's not very good practice.Quote:
I though you had to declare all array sizes at compile time
You could either manage your arrays dinamically using new and delete ,use something like the c++ vector template or use your own data structure.
Thanks for backing me up zen, but that still dosent answer my question.
how can i read this line of numbers from a file
1 3 2 4 5 6 3
and then this line useing the same code
23 1 4 5 32 2 5
If i just read in one charcter at a time, double digits will throw me off.
Whats the answer?
I just hink you need to think for yourself a little.Code:#incude <fstream.h>
void main()
{
int n1,n2,n3,n4,n5;
ifstream file("blah.txt");
file>>n1>>n2>>n3>>n4>>n5;
file.close();
}
SPH:rolleyes:
I'm not a C++er so this could be improved but I would think that you would want to use an array for this or else a vector.Code:#include<iostream>
#include <fstream>
using namespace std;
int main()
{
int num[10];
int i = 0;
ifstream file("text.txt");
while(file>>num[i++]);
file.close();
--i;
while(i)
{
cout << num[--i] << endl;
}
return 0;
}
Maybe I'll try to come up with the vector code.
This is more the C++ers way!Code:#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
vector<int> v_numbers;
int x;
ifstream file("text.txt");
while(file >> x) v_numbers.push_back(x);
file.close();
for(vector<int>::size_type size = 0; size < v_numbers.size(); ++size)
{
cout << v_numbers[size] << endl;
}
return 0;
}
I'm a C++er and i still don't know crap about vectors.... oooooo
i just remembered the encryption system that i was going to program today.... *opens up dev-cpp and starts coding*
Read up on the different istream member functions/operators like >> and get() and getline(). They are used in both cout and any user defined streams like those used to read in files, etc. In a nutshell I use the >> operator if I want to read in a discrete variable delimited from other variables by whitespace and getline() when I want to read in an entire string that may (or may not) have embedded whitespace, parsing the input string when necessary.
Thanks alot guys, I think I got it now.