How to Ifstream codeblocks on mac? - c++

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("test.txt");
int foo;
string sFoo;
inFile >> sFoo;
inFile >> foo;
cout << "the name is " << sFoo << endl;
cout << "the first number is " << foo << endl;
inFile >> foo;
cout << "the second number is " << foo << endl;
cout << "Hello World!";
return 0;
}
I have tried putting my text file in the same folder. However, for some reason it is not able to read the text file. Please can someone tell me what to do in codeblocks on macbook to make this happen!

You have to write full absolute path of your file and not relative. I've answered the same question here.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
inFile.open("/Users/user/Desktop/test.txt");
if(inFile){
int foo;
string sFoo;
inFile >> sFoo;
inFile >> foo;
cout << "the name is " << sFoo << endl;
cout << "the first number is " << foo << endl;
inFile >> foo;
cout << "the second number is " << foo << endl;
cout << "Hello World!";
inFile.close();
}else{
cout<<"unable to open file"<<endl;
}
return 0;
}

Related

Incomplete type is not allowed in my function call

I am getting an error in my code when trying to call a function. I have tried this a few different ways, but I just can't get it to work and I know it must be something stupid.
#include <iostream>
#include <fstream>
using namespace std;
void openFile(string input);
int main()
{
string input;
cout << "Please enter a file to open: " << endl;
cin >> input;
void openFile(input); //error is right here!!!!!!!!!
return 0;
}
void openFile(string input) {
ifstream in_file;
in_file.open(input);
if (in_file.fail())
cout << "Something went wrong, file did not open!!!" << endl;
else {
cout << "File opened successfully!!!" << endl;
cout << in_file.rdbuf() << endl;
}
in_file.close();
}
You shouldn't write a void for calling a function it's a return type used for functions prototype or deceleration. Below is correct code.
int main()
{
string input;
cout << "Please enter a file to open: " << endl;
cin >> input;
openFile(input); // function calls must not be preceded with a void keyword
return 0;
}

Incorrectly returning a value from a function in C++

>
Code:
int main ()
{
ifstream inStream;
ofstream outStream;
getInputOutputStreams(inStream, outStream);
numberFile(inStream, outStream);
return EXIT_SUCCESS;
}
I have been stuck with an issue where the code I wrote compiles, but when running it has to be terminated because it just lags. I believe my issue may have to deal with not correctly returning the names of the input and output files, but I cannot figure out where I'm going wrong. I'm only a beginner in C++ (we are just now learning about arrays), so if you think this is a dumb question I'm sorry!
Here is the code I have written:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
string getInputOutputStreams(ifstream &inStream, ofstream &outStream);
void numberFile(ifstream &inStream, ofstream &outStream);
int main ()
{
ifstream inStream;
ofstream outStream;
getInputOutputStreams(inStream, outStream);
numberFile(inStream, outStream);
return EXIT_SUCCESS;
}
string getInputOutputStreams(ifstream &inStream, ofstream &outStream)
{
string inputFile;
string outputFile;
cout << "Enter the name of the input file:" << endl;
cin >> inputFile;
inStream.open(inputFile);
while (inStream.fail()) {
cout << "Invalid file name." << endl;
cout << "Enter the name of an input file:" << endl;
cin >> inputFile;
inStream.open(inputFile);
}
inStream.close();
cout << "Enter the name of the output file:" << endl;
cin >> outputFile;
outStream.open(outputFile);
while (outStream.fail()) {
cout << "Invalid file name." << endl;
cout << "Enter the name of an output file:" << endl;
cin >> outputFile;
outStream.open(outputFile);
}
outStream.close();
return inputFile;
return outputFile;
}
void numberFile(ifstream &inStream, ofstream &outStream)
{
string inputFile;
string outputFile;
inStream.open(inputFile);
outStream.open(outputFile);
int lineNumber = 0;
string line;
while(!inStream.eof()) {
if(line == " ") {
}
else {
lineNumber++;
outStream << lineNumber << ": " << line << endl;
}
getline(inStream, line);
}
cout << lineNumber << " lines processed" << endl;
inStream.close();
outStream.close();
}
I see a number of mistakes:
getInputOutputStreams() is closing the streams that it opens.
incorrect return statements in getInputOutputStreams().
numberFile() is re-opening the streams, using filename strings that have no values.
using while(!inStream.eof()) is bad.
not ignoring blank lines, as the instructions ask for.
Try this instead
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void getInputOutputStreams(ifstream &inStream, ofstream &outStream);
void numberFile(ifstream &inStream, ofstream &outStream);
int main ()
{
ifstream inStream;
ofstream outStream;
getInputOutputStreams(inStream, outStream);
numberFile(inStream, outStream);
return EXIT_SUCCESS;
}
void getInputOutputStreams(ifstream &inStream, ofstream &outStream)
{
string inputFile;
string outputFile;
cout << "Enter the name of the input file:" << endl;
cin >> inputFile;
inStream.open(inputFile);
while (inStream.fail()) {
cout << "Invalid file name." << endl;
cout << "Enter the name of an input file:" << endl;
cin >> inputFile;
inStream.open(inputFile);
}
cout << "Enter the name of the output file:" << endl;
cin >> outputFile;
outStream.open(outputFile);
while (outStream.fail()) {
cout << "Invalid file name." << endl;
cout << "Enter the name of an output file:" << endl;
cin >> outputFile;
outStream.open(outputFile);
}
}
void numberFile(ifstream &inStream, ofstream &outStream)
{
int lineNumber = 0;
string line;
while (getline(inStream, line)) {
if (line != "") {
++lineNumber;
outStream << lineNumber << ": " << line << endl;
}
}
cout << lineNumber << " lines processed" << endl;
}

error: request for member which is of non-class type 'MULTIMEDIA_FILME [500]'| [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
So the main objective of the program was to build a file and save on it the data I would input.
I did this with char and worked perfectly but I wanted to do with string too, so I tried the way I did with char and it gives me some errors
error: request for member 'nome' in 'filmes', which is of non-class type 'MULTIMEDIA_FILME [500]'|
and the same for every string.
I tried deleting the [500] and the [1] in each string and it works, but I wanted to add and save various datas for the same strucure like:
Nome: "some name"
Tipo: "some type of movie"
Ano: "some year"
Descricao: "some description"
....
and repeat it once I do other input.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct MULTIMEDIA_FILME
{
string nome;
string tipo;
int ano;
string descricao;
};
int main()
{
using namespace std;
struct MULTIMEDIA_FILME filmes [500] ;
fstream myFile("filme.txt", ios::in | ios::out | ios::app);
ofstream filme_txt;
filme_txt.open("filme.txt");
cout << "Nome do filme:" << endl;
cin >> filmes.nome[1];
filme_txt << filmes.nome[1] << endl;
cout << "Tipo do filme:" << endl;
cin >> filmes.tipo[1];
filme_txt << filmes.tipo[1] << endl;
cout << "Ano do filme:" << endl;
cin >> filmes.ano[1];
filme_txt << filmes.ano[1] << endl;
cout << "Descricao do filme:" << endl;
cin >> filmes.descricao[1];
filme_txt << filmes.descricao[1] << endl;
filme_txt.close();
}
EDIT1:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct MULTIMEDIA_FILME
{
string nome;
string tipo;
int ano;
string descricao;
};
int main()
{
int n_filmes=0,contador,i=0;
struct MULTIMEDIA_FILME filmes [500] ;
FILE *ficheiro_num_filmes;
ficheiro_num_filmes=fopen("num_filmes.txt","rb");
if(ficheiro_num_filmes==NULL)
{
ficheiro_num_filmes=fopen("num_filmes.txt","wb");
fwrite(&n_filmes,sizeof(int),1,ficheiro_num_filmes);
fclose(ficheiro_num_filmes);
}
else
{
fread(&n_filmes,sizeof(int),1,ficheiro_num_filmes);
fclose(ficheiro_num_filmes);
}
fstream myFile("filme.txt", ios::in | ios::out | ios::ate | ios::app);
ofstream filme_txt;
filme_txt.open("filme.txt");
cout << "Quanto filmes deseja gravar:" << endl;
cin >> contador;
cout << "Nome do filme:" << filmes[1].nome << endl;
do{
cout << "Nome do filme:" << n_filmes+1 << endl;
cin >> filmes[1+n_filmes].nome;
filme_txt << filmes[1+n_filmes].nome << endl;
cout << "Tipo do filme:" << endl;
cin >> filmes[1+n_filmes].tipo;
filme_txt << filmes[1+n_filmes].tipo << endl;
cout << "Ano do filme:" << endl;
cin >> filmes[1+n_filmes].ano;
filme_txt << filmes[1+n_filmes].ano << endl;
cout << "Descricao do filme:" << endl;
cin >> filmes[1+n_filmes].descricao;
filme_txt << filmes[1+n_filmes].descricao << endl;
n_filmes ++;
i++;
}while(i<contador);
filme_txt.close();
ficheiro_num_filmes=fopen("num_filmes.txt","w+b");
fwrite(&n_filmes,sizeof(int),1,ficheiro_num_filmes);
fclose(ficheiro_num_filmes);
}
The program has two problems:
The program declares 500 filmes objects. This means that when you want to access the objects, the subscript should be provided prior to the member, e.g.
filmes.nome[1]
should be
filmes[1].nome
There is no need to declare 500 objects. Unless there is a requirement to create 500 different files, or to be able to store 500 different film objects, only one film object need be created. So, a simplified version of the program would be:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct MULTIMEDIA_FILME {
string nome;
string tipo;
int ano;
string descricao;
};
int main() {
struct MULTIMEDIA_FILME filmes;
fstream myFile("filme.txt", ios::in | ios::out | ios::app);
ofstream filme_txt;
filme_txt.open("filme.txt");
cout << "Nome do filme:" << endl;
cin >> filmes.nome;
filme_txt << filmes.nome << endl;
cout << "Tipo do filme:" << endl;
cin >> filmes.tipo;
filme_txt << filmes.tipo << endl;
cout << "Ano do filme:" << endl;
cin >> filmes.ano;
filme_txt << filmes.ano << endl;
cout << "Descricao do filme:" << endl;
cin >> filmes.descricao;
filme_txt << filmes.descricao << endl;
filme_txt.close();
}
If you truly wish to see 500 films stored in a single file, then you need to add some sort of while() loop to query up to 500 times for the film objects. As your requirement did not request this, and as there is no loop in your program, I have not tried to debug this fork of the problem.
Here is updated code with a for loop in response to your comment. Much more can be done. The requirements are not clear as to when you want to overwrite the file(s) and when you want to append, and when you want to read back the contents of the file. I suspect those are additional requirements that will come later.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Multimedia_Filme {
string nome;
string tipo;
int ano;
string descricao;
};
int GetFilmCountFromFile(const string &fileName) {
ifstream myFile;
myFile.open(fileName);
if (myFile.is_open()) {
string line;
getline(myFile, line);
return stoi(line); // convert string to int
} else {
return 0;
}
}
void WriteFileCountToFile(const string &fileName, int fileCount) {
ofstream myFile;
myFile.open(fileName);
if(myFile.is_open()) {
myFile << fileCount << endl;
myFile.close();
}
}
int GetIntFromUser(string prompt) {
int count;
cout << prompt << endl;
cin.clear();
cin >> count;
return count;
}
string GetStringFromUser(string prompt) {
string returnString;
cout << prompt << endl;
cin.clear();
cin.ignore(1);
cin >> returnString;
return returnString;
}
int main(int argc, char **argv) {
int n_filmes = 0, contador, i = 0;
Multimedia_Filme filmes[500];
n_filmes = GetFilmCountFromFile("filmCountFile.txt");
contador = GetIntFromUser("Quanto filmes deseja gravar: ");
ofstream filmeFile;
filmeFile.open("filme.txt");
if(filmeFile.is_open()) {
for (int i = 0; i < contador; i++) {
filmes[i].nome = GetStringFromUser("Nome do filme: ");
filmes[i].tipo = GetStringFromUser("Tipo do filme: ");
filmes[i].ano = GetIntFromUser("Ano do filme: ");
filmes[i].descricao = GetStringFromUser("Descricao do filme: ");
filmeFile << filmes[i].nome << endl
<< filmes[i].tipo << endl
<< filmes[i].ano << endl
<< filmes[i].descricao << endl;
}
filmeFile.close();
}
WriteFileCountToFile("num_filmes.txt", contador);
}

Reading from a file and displaying it backwards

I'm trying to write a program to read from a file and display the text backwards. - My loop backward loop is not working. Any suggestions?
- Also if I'm reading a file that only contain floats integers or floats, how would I display them all as float?
Thanks,
#include <iostream>
#include <fstream>
using namespace std;
void seeReverseText(char fileName[])
{
ifstream fin(fileName);
if (fin.fail())
{
cout << "Error opening file " << fileName << endl;
return;
}
cout.setf(ios::showpoint);
cout.precision(2);
cout.setf(ios::fixed);
int i = 0;
cout << "New order:\n";
while (!fin.eof())
{
// this is what I was trying to do
// i++;
// for (i--; i >= 0; i--)
// fin >> fileName[i];
// cout << "' " << fileName[i] << "'";
fin >> fileName;
cout << fileName << endl;
}
fin.close();
}
int main()
{
char fileName[256];
cout << "Enter the filename: ";
cin >> fileName;
seeReverseText(fileName);
return 0;
}
Try something more like this instead:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
void seeReverseText(const std::string &fileName)
{
std::ifstream fin(fileName);
if (!fin)
{
std::cout << "Error opening file " << fileName << std::endl;
return;
}
std::cout.setf(std::ios::showpoint);
std::cout.precision(2);
std::cout.setf(std::ios::fixed);
std::cout << "New order:\n";
std::string line;
while (std::getline(fin, line))
{
std::reverse(line.begin(), line.end());
std::cout << line << std::endl;
}
}
int main()
{
std::string fileName;
std::cout << "Enter the filename: ";
if (std::cin >> fileName)
seeReverseText(fileName);
return 0;
}

Reading from text file with ifstream

So I am trying to read from a text file. It shows that I can successfully read from the file But when I try to cout the values, it just shows 0, while I have other values in the text file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
std::ifstream file("numbers.txt");
if (file) {
cout << "Managed to read file successfully. \n";
}else{
cout << "Unable to read file.";
}
int x, y;
file >> x >> y;
cout << "Num 1: " << x << endl;
cout << "Num 2: " << y << endl;
return 0;
}
With the numbers.txt file
45
15
Your code work find with gcc.
int main()
{
std::ifstream file("numbers.txt");
if (file)
{
cout << "Managed to read file successfully. \n";
int x, y;
file >> x >> y;
cout << "Num 1: " << x << endl;
cout << "Num 2: " << y << endl;
}
else
{
cout << "Unable to read file.";
}
return 0;
}
The above code will print status of your operation,if you want to read and display contents the you have to write your code like this:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream stream1("D:\\file1.txt");
char a[80];
if(!stream1)
{
cout << "While opening a file an error is encountered" << endl;
}
else
{
cout << "File is successfully opened" << endl;
}
while(!stream1.eof())
{
stream1 >> a;
cout << a << endl;
}
return(0);
}