Here is a simple program to output to a text file:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
double myNumber = 42.5;
fstream outfile("test.txt", fstream::out);
outfile << "The answer is almost " << myNumber << endl;
outfile.close();
}
All that ends up being wrote to my text file is, "The answer is almost " and the data is not displayed at all. What am I doing wrong? or could it be a problem with Xcode since I am using that as an IDE.
I'm not sure what the problem is. Is it that it's never executed or that it's writing to the wrong path. To shed light on this try include unistd.h and insert this snippet.
char* s = getcwd(NULL, 256);
printf("im running and pwd is: %s\n", s);
Inside xcode hit CMD-SHIFT-R to open the console and see if it prints anything.
There is no problem with your code. It could be a problem with Xcode.
Related
I have been writing code like this to display cyrillic text on the console and it has always worked, but it suddenly stopped working for some reason, I don't understand why. What's the problem?
This is on Visual Studio, Windows
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
//SetConsoleCP(1251);
//SetConsoleOutputCP(1251);
ifstream input{ "in_text.txt" };
if (!input) {
cerr << "Error opening file" << endl;
return 1;
}
cout << "Displaying file contents: " << "\n\n";
string line{};
while (getline(input, line))
cout << line << endl;
input.close();
return 0;
}
I've previously been using setlocale, I've also now tried the windows SetConsoleCP nothing is working. This is the output everytime:
Displaying file contents:
Р?С?РёР?РчС'
Also, if there is a better way to output cyrillic text on the console, please let me know.
Windows OS sometimes behave strangely and unpredictable when it comes to cyrillic in console
You can try SetConsoleOutputCP(CP_UTF8);
it helped me once(<windows.h> header required)
My code is supposed to open a file and work with the data that it reads in. My code looks like this:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
ifstream input_file;
input_file.open("practice.txt");
if (input_file.fail())
{
cout << "Attempt to open file failed." << endl;
}
else
It always returns "attempt to open file failed". I am definitely using the correct file name, so what are some reasons that the file isn't opening?
How do I solve this issue?
EDIT: spoke to another girl in my class with a Mac and her code works perfectly when not run on Mac but won't open the file when she runs it on her computer so I think it's a problem with my compiler. Thanks for the help!!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I can not get Visual Studio to read in a text file. Below is the code I have. The file opens perfectly in a Unix Environment, but it does not work when copied and pasted into Visual Studio. I am using fstream to open the file. Why the file is not being read?
When I run the program, it builds but I get no output. I have an output statement cout << "inf\n". So the loop is not even being reached, which is why I believe the file is not being read. Again, when I run the same code in a Unix environment the output statement does display and the values from the file are displayed ( via tree.insert(), tree.remove() ).
I tried the solution in this link. As it suggested, I changed my working directory to $(ProjectDir)\Debug and $(ProjectDir)\Release. Also, I moved my text file from the Resources folder to my Source Folder in the Solution Explorer. However, the file still was not being read.
I also updated my code to include cerr << "Error: " << strerror(errno); directly after fstream inf ("BTREE5_1.txt"). With this line of code the output I get is
Error: No such file or directory
Can someone please explain why? My text files are in the same folder as my code as explained above.
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include "BTree.h"
using namespace std;
int main()
{
bool first = true;
BTree tree(6, 2);
int value;
char s[80], command;
ifstream inf("BTree5_1.txt");
cerr << "Error: " << strerror(errno);
inf.getline(s, 80);
while (inf >> command >> value)
{
cout << "inf\n";
if (command == 'i')
tree.insert(value);
else
{
if (first)
{
cout << "After all insertions.\n";
tree.print();
first = false;
} // if first
cout << "Deleting " << value << ". \n";
tree.remove(value);
tree.print();
// fgets(s, 80, stdin);
} // else deletion
} // while
system("PAUSE");
return 0;
} // main
The problem was that I copied and pasted my text files from the Unix Environment. To fix this I just placed the text files into my Directory from my C Drive.
ie>) C:\Users\s.proctor\Documents\Visual Studio 2015\Projects\ecs60\p2\p2\p2
Previously, I have been interested in learning C++ so I decided to go for "InfiniteSkills" training video (http://www.infiniteskills.com/training/learning-c-plus-plus.html)
The instructor start by teaching "Hello World" as a basic as always.
Here is the code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
but after I build it using CodeBlocks it won't compile
I have also tried using Sublime text too, but the result seems to be the same
Any suggestion?
Image:
you should add a newline character to the end of the line you want to print. Probably you are not seeing your output because it is still in the buffer. As #Quirliom noted: It may not be the stdio buffer but Sublime buffering until new lines...
cout << "Hello, World!\n";
or
cout << "Hello, World!" << endl;
As per the comments, you are unable to see the output. Try this:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
cin.get(); // This waits for you to input something and allows you to see the input.
return 0;
}
I don't know the real solution for this problem. But my guess is because of the complier. I have test with CodeBlocks and Sublime Text 3 on mac both won't print "Hello World" for me. So I decided to test with another which is "Xcode" and it works! I don't know what the real problem is but if anyone have any problem like me you may want to try using another complier :)
Thank you everyone for your suggestion and happy coding!!!!
You should both add a newline command in your print function and some sort of pause.
#include <iostream>
using namespace std;
int main(){
cout << "Hello World!\n" //calls for a newline
cin.get(); //pauses until a key is pressed
return 0;
}
Try this and see if it works
I had this problem too but I was able to fix it by reinstalling the C++ plugin for VS Code. I think the iostream wasn't actually there originally.
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;
}