Okay, so I am supposed to write code that allows user input for inventory data that will but stored in a text file on a disk with each items data on a single line. The prompts for the user need to be:
stock number with a maximum of 5 chars
item description with a maximum of 20 chars
cost
price
and quantity in stock
The first problem i have is creating a file. I created a test, and the build succeeded (I am using Xcode by the way) but i cannot find a file that it wrote to. I'm not entirely sure whats wrong. Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <fstream>
#include <iomanip>
usingnamespace std;
int main ( )
{
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
The file will be created in the working directory of the executable that is your program. That directory is almost certainly not the same as your "project" directory.
I can’t spot any errors in your code too.
You could try to:
a) add some code that warns you if there are problems (for example, you could be not allowed to write on some directory):
b) Change the name of your file in something strange and unique, then launch a global search on your hard disks (sooner or later you’ll find it!): std::ofstream myfile("sea2017_jdnfoiuw.txt");
c) Compile it from console: it will create the executable in the same directory of your source file.
Assuming your source file is named ‘main.cpp’, you can try this (GCC compiler: for Microsoft Visual Studio the command is different): g++ -std=gnu++17 -Wall -Wextra -pedantic-errors main.cpp -o main.exe
d) Give us more information about your environment (operative system, programming suite...): that will help us to give more accurate directions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iomanip>
#include <iostream>
#include <fstream>
int main ( )
{
std::ofstream myfile("sea2017_jdnfoiuw.txt");
if(!myfile) {
std::cout << "Can't create the file.\nExiting now.\n";
myfile.close();
return 1;
}
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
I use GNU/Linux with vim and Makefiles but I think that in Windows most IDEs compile the project to 'release' or 'debug' (if I recall correctly) however when you run the program from an IDE it gets copied to a temporary location like C:\Users\Melinda\AppData\Roaming\Temp\a.exe and runs from there.
Perhaps that's what happens with Xcode too.
Since Xcode is an OS X program, perhaps you can save it in your Home directory instead.
myfile.open ("~/example.txt");
I assume that HFS+ filesystem resembles somewhat a standard Unix filesystem (I know a few differences though).