I have some coding experience but am relatively new to c++. I am quite simply trying to read in some numbers I have in a .txt file. They are in a column in the file (sepereated by an "Enter"), does that make any difference? I had a go at writing a program and it compiles but gives me nothing. I'm just wondering if someone might be able to point me in the right direction. Thanks in advance! :-)
#include<iostream>
#include<fstream>
usingnamespace std;
int main () {
ifstream myReadFile;
myReadFile.open("myfile.txt");
char output[300];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile >>output;
std::cout<<output;
}
}
myReadFile.close();
return 0;
}
I would ideally like to be able to select a particular line in the .txt file then manipulate the value and output it to a new file. If anyone could help with that bit too it would be amazing also.
Thanks for the code. Unfortunately all it gave me was;
error: 'strerror' was not declared in this scope.
Does that mean I don't have the library for 'strerror' installed?
Also I am expecting the program to print the contents of the .txt file in the terminal. Is that what I should be expecting? I know these are quite fundamental questions but hitting bit of a brick wall here :-)
I'm using Linux and when I included cerrno, that was the error that it gave me. Otherwise there doesnt appear to be any error. When it complies and I run the program it just returns me to the command prompt. I've checked the .txt file and it definitely contains data.
I'm now wondering how to get that data that is contained in "output" into an array so I don't need to keep opening up files which is slowing my program down quite a lot.
Basically what I would like to do it turn the .txt file into an array.
I'm sure this is easy, just currently trawling the internet for a way to do it that is like mine. Any help? :-)
First off, you're doing some weird things with brackets. But, numbers is not declared in main, it's only in your read_from_file() function, which is also never even used. Your program always starts at main(), so you need to have a function call in there somewhere.
You print out specific elements just how you're doing it. Numbers[0] prints out the first element, the number in te brackets is the index of the element you want, starting at 0.
As I understand it I am now creating a vector called 'printnumbers' in main which I then populate with the data from the 'read_from_file' function and then print. I accept my previous attempt was pretty much completely stupid but where am I going wrong with this? :-s
printnumbers = read_from_file();
Forgot the parentheses. Compiler is gonna think you're referring to a variable if you forget that. And you have a parameter to worry about.
Have you even tried to compile this?
vector printnumbers;
As far as I know, this won't work. Vector is not a data type.
vector<type> name; Should be the way to declare a vector.
I have included the corrections you stated and yea, I have tried to compile it. I am currently getting the error:
filename.cpp: In function 'int main()':
filename.cpp:9: error: too few arguments to function 'std::vector<int, std::allocator<int> > read_from_file(const char*)'
filename.cpp:23:error: at this point in file
I'm guessing these two errors are both part of the same problem. I'll have a look around the net to see if I can find any answers.
I now have the code compiling but whenever I run the program I get a different integer that bares no resemblance to what is in the .txt file. eg:
3548992
6022976
2537280
The number clearly should not be changing each time I run the program as it should be displaying the value of the first element in the vector. I have had this problem before when writing code in another language but was so long ago and now I can't remember what the problem was. Here is a copy of my code if you want to have a look.