I am trying to read a .txt file with some floats into my code.
I wrote a sample code just to tackle the issue outside my main code and I am using the following floats to test it:
10.8f
100.8f
-10.8f
The issue I am running into is that the code only reads in the first float properly and displays it but all the other floats following it do not look correct:
10.8
0
4.57874e-41
Code:
#include<fstream>
#include<iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
float Cam1,Cam2,Cam3;
string path = "sample.txt"; //Text file with above mentioned floats
ifstream fin;
fin.open(path);
if(fin.is_open())
{
fin >> Cam1;
fin >> Cam2;
fin >> Cam3;
fin.close();
}
cout << Cam1 << '\n';
cout << Cam2 << '\n';
cout << Cam3 << '\n';
}
I really am confused as to why it reads the first properly but not the others, it works when I change the values as well, the above is just one example case. I am fairly new to C++ so any help would be greatly appreciated thank you!
The f suffix is valid in C++ code, but not in input text parsed by istream. It's useful in code to distinguish between float and double constants, but user input doesn't control variable data types.
Related
I wrote a program that takes its input from a file (using ifstream), uses a bunch of std::getline to extract the data and manipulate it.
I want to use I/O redirection so that these lines can be used using std::cin (As if someone is typing this info). I looked it up but I didn't quite understand how I would implement it using the Visual Studio Community program. Any help is greatly appreciated.
I would suggest you to use a stringstream for that:
#include <iostream>
#include <sstream>
int main() {
string z = "100";
stringstream a("");
a << z;
int x;
a >> z; // Extracts data; a works like cin would have if it were used to input data
cout << z;
return 0;
}
I found the solution to my question!
I simply changed the following line of code from:
std::ifstream datastream(text file location);
while (std::getline(datastream, output_str)) {
.
stuff...
.
}
To:
while (std::cin(datastream, output_str));
Once I compiled my code with no errors and got my ./a.out, I typed in my compiler:
./a.out < text file
And that pretty much started inputting the file content into std::cin as if someone was typing it.
Here is what I got so far:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int characterList = 0;
char* dynamo = new char[1000];
char* buffer = dynamo;
ifstream input("wordlist.txt");
if (input.is_open())
{
input >> dynamo[characterList];
while (input.eof())
{
characterList++;
input >> dynamo[characterList];
cout << dynamo[characterList];
}
}
else
{
cout << "File not opened" << endl;
}
return;
}
I'm a beginner so I do apologize if this looks like terrible coding practice. I created a text file with a quote from Bill Cosby that I'm trying to read one word at a time. The quote is "I don't know the key to success, but the key to failure is trying to please everybody." I'm trying to read one word at a time from a text document ignoring punctuation. I know there are a lot of questions similar to this, but they are using code that I have not learned so I'm sorry for having a repeating question. I have not learned getline (I used cin.getline) and #include <string>.
Edit: I forgot to mention, so I'm sorry for not doing so earlier, but I'm studying dynamic memory allocation which is why I'm using the new char[1000].
I'd suggest you to use std::string instead of manually allocating buffers on the heap with new[] and trying to read text manually from the file into those buffers (and don't forget to free the buffer with proper delete[] calls!).
C++ input stream classes like std::ifstream can simply read text into std::string instances thanks to a proper overload of operator<<.
The syntax is as simple as:
string word;
while (inFile >> word)
{
cout << word << endl;
}
Here's a complete compilable sample code for you to experiment and learn:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream inFile("test.txt");
if (inFile.is_open())
{
string word;
while (inFile >> word)
{
cout << word << endl;
}
}
else
{
cout << "Can't open file." << endl;
}
}
This is the output I got on a test text file having the content specified in your question:
I
don't
know
the
key
to
success,
but
the
key
to
failure
is
trying
to
please
everybody.
NOTE
Of course, once you have your words read into a std::string instance, you can store them in a container like std::vector<std::string>, using its push_back() method.
I would do something like this:
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::string array[6];
std::ifstream infile("Team.txt");
std::string line;
int i = 0;
while (std::getline(infile, line)) {
array[i++] = line;
}
return 0;
}
based on this answer.
Here, we assume we have to read 6 lines from the file "Team.txt". We use std:::getline() and we put inside a while so that we read all the file.
At every iteration, line holds the current line of the file read. Inside the body we store it in array[i].
I have made a scripting language and I was writing a program which would 1: Ask for your path to read the script and 2: Compile the code into a custom Hexadecimal which would be read by another program to do the hard work for me. My problem is I cannot create a new file and write the custom hexadecimal code onto that file.
Here is an example of my code:
#include <iostream>
#include <string>
#include <Windows.h>
#include <wincon.h>
#include <fstream>;
using namespace std;
int main(){
string path;
string title;
cout << "Enter your path to compile your *.egSCRIPT file: ";
cin >> path;
cout << "Enter the title for the new file";
cin >> title;
ofstream myfile(path+"\\"+title+".myScriptLanguage");
myfile.open(path);
myfile << "IT WORKS!!!";
myfile.close();
return 0;
}
I want it to make a new .whatevertheheckyouwanttocallit file and write onto the file IT WORKS!!! just as a sample. I will eventually write onto it a custom hexadecimal system of code to be read by my interpreter. Thanks in advance.
ofstream myfile;
myfile.open(path+"\\"+title+".myScriptLanguage");
myfile << "IT WORKS!!!";
myfile.close();
Try the above;
I am working on a project where I intend on connecting to a database, grabbing a .csv file, reading it, manipulating the data and then returning it back to the database. Fairly simple and straight forward but I am still learning so if any one could point me in the right direction I would greatly appreciate it. Right now I have a simple program that is trying to read a .csv file and return the values to me printed on the console. I have been trying to find some good online resources for this but have came up short. Here is my code for what I have stumbled through so far.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
int loop = 1;
while(loop = 1)
{
cout << "Welcome! " << endl;
ifstream myfile;
myfile.open ("..\\ Source\External\\ Sample.csv", ifstream::in);
The real path to this file is C:\Documents and Settings\RHatfield\My Documents\C++\Product Catalog Creator\Source\External\Sample.csv
while (myfile.good())
cout << (char) myfile.get();
myfile.close();
system("PAUSE");
return 0;
}
}
Now the issue is it does not print the values so I do not know that they are properly being captured. I have a feeling it is my file path but that's the only way I can find to write it without it throwing errors. I think it's something with the spaces in the file path but I can't seem to find another way to make it work. I am not looking for a handout and this is not homework or just regular work. I am trying to learn and having trouble teaching myself so if someone knows what the issue is and can help me fix it or even point me to a relevant article online would be greatly appreciated.
Try the following code. I think the problem was you were making an assignment statement in the while condition statement.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
cout << "Welcome! " << endl;
ifstream myfile;
myfile.open ("C:\\Documents and Settings\\RHatfield\\My Documents\\C++\\Product Catalog Creator\\Source\\External\\Sample.csv", ifstream::in);
while (myfile.good())
cout << (char) myfile.get();
myfile.close();
system("PAUSE");
return 0;
}
I'm new to C++, and I'm trying to write a short C++ program that reads lines of
text from a file, with each line containing one integer key and one alphanumeric string value (no embedded whitespace). The number of lines is not known in advance, (i.e., keep reading lines until end of file is reached). The program needs to use the 'std::map' data structure to store integers and strings read from input (and to associate integers with strings). The program then needs to output string values (but not integer values) to standard output, 1 per line, sorted by integer key values (smallest to largest). So, for example, suppose I have a text file called "data.txt" which contains the following three lines:
10 dog
-50 horse
0 cat
-12 zebra
14 walrus
The output should then be:
horse
zebra
cat
dog
walrus
I've pasted below the progress I've made so far on my C++ program:
#include <fstream>
#include <iostream>
#include <map>
using namespace std;
using std::map;
int main ()
{
string name;
signed int value;
ifstream myfile ("data.txt");
while (! myfile.eof() )
{
getline(myfile,name,'\n');
myfile >> value >> name;
cout << name << endl;
}
return 0;
myfile.close();
}
Unfortunately, this produces the following incorrect output:
horse
cat
zebra
walrus
If anyone has any tips, hints, suggestions, etc. on changes and revisions
I need to make to the program to get it to work as needed, can you please
let me know?
Thanks!
See it:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string name;
int value;
ifstream myfile("text.txt", ifstream::in);
while(myfile >> value >> name)
cout << name << endl;
return 0;
}
You are having problems because you attempt to read each line twice: first with getline and then with operator>>.
You haven't actually used std::map in any regard, at all. You need to insert the integer/string pair into the map, and then iterate over it as the output. And there's no need to close() the stream.
Instead of using "! myfile.eof()" use this code it will help.
ifstream is;
string srg;
is.open(filename);
while(getline(is,srg))
{//your code
}