Putting a String from a text into an array, using scanf - c++

I'm having some problems reading a string into an array. my file contains the following strings running horizontally down the page.
File:
dog
cat
rabbit
mouse
Code:
#include <string>
int i = 0;
using namespace std;
int main()
{
FILE * input1;
fopen_s(&input1, "C:\\Desktop\\test.dat", "r");
string test_string;
while (!feof(input1)) {
fscanf_s(input1, "%s", test_string);
i++;
}
return 0;
}
Any advice would be appreciated, Thanks!

You should use ifstream and std::getline
Now, I'm going to walk you through reading lines from the file using ifstream
Include fstream to use ifstream.
#include <fstream>
Opening a file:
To open a file, create an object of ifstream, and call it's method open and pass the filename as parameter. ifstream opens a file to read from it. (To write in a file, you can use ofstream)
ifstream fin;
fin.open("C:\\Desktop\\test.dat");
Or you can simply pass the filename to the constructor to create an object of ifstream and open a file.
ifstream fin("C:\\Desktop\\test.dat");
Reading from the file:
You can use stream extraction operator (>>) to read from the file, just like you use cin
int a;
fin >> a;
To read a line from a file using the above created fin (using a char array)
char arr[100];
fin.getline(arr, 100);
Better yet, you should use std::string instead or char arrays, using std::string, you can read a line using std::getline
string testString;
getline(fin, testString);
Now, let's change your code to use ifstream and getline
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int i = 0;
ifstream input1;
input1.open("C:\\Desktop\\test.dat");
string test_string;
while (getline(input1, test_string)) {
i++;
}
return 0;
}

Related

I tried to write the code to count the number of characters in the line of the file but the ifstream object doesnot take the line from the file

This code doesnot take string of file count in ofstream object file1.
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ofstream file1("count.txt");
string sentence;
getline(cin,sentence);
file1<<sentence;
ifstream file2("count.txt");
string wordscount;
while(getline(file2,wordscount))
{
cout<<wordscount;
}
}
You did not close the stream, so the file may be not existing on the FS when new stream file2 is opened. Add the line
file1.close(); // <-- new line
ifstream file2("count.txt");

Read Multiple Files In C++

I am trying to read two files "ListEmployees01.txt" and "ListEmployees02.table". But the program reads only the "ListEmployees01.txt" file and cout is just from that file.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
int main()
{
freopen("ListEmployees01.txt", "r", stdin);
string s;
while (getline(cin, s))
cout << s<<endl;
fclose(stdin);
freopen("ListEmployees02.table", "r", stdin);
while (getline(cin, s))
cout << s<<endl;
}
You can use std::ifstream instead of changing std::cin's behavior.
I would do the following using fstream
#include <fstream>
void readAndPrint(const char *filename) {
std::ifstream file(filename);
if (file.is_open()) {
std::string line;
while (getline(file, line)) {
printf("%s\n", line.c_str());
}
file.close();
}
}
int main() {
readAndPrint("ListEmployees01.txt");
readAndPrint("ListEmployees02.table");
return 0;
}
If you must use freopen, then have a look at man freopen, or the C++ reference http://www.cplusplus.com/reference/cstdio/freopen .
In your case , in the case of second file you are using the stdin which is already closed by below line , hence it is a dangling pointer after file close
fclose(stdin)
You can use fopen instead of freopen for the second file.
Please check the below paragraph from www.cplusplus.com/reference/cstdio/freopen/
If a new filename is specified, the function first attempts to close
any file already associated with stream (third parameter) and
disassociates it. Then, independently of whether that stream was
successfuly closed or not, freopen opens the file specified by
filename and associates it with the stream just as fopen would do
using the specified mode.

Read Excel file in C++ J2ME and get data from itQ

How can I open an Excel file in C++ J2ME and get data from it or store data in it?
This is my (miniscule) approach so far:
#include <fstream>
ifstream inFile;
inFile.open("customer.dat");
try this code
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream filestore("customer.dat"); //store data in file
string data1; //store text
int data2; //store integers
cout<<"enter data or press ctrl+z to quit \n";
while(cin>>data1>>data2){
filestore<<data1<<endl;
filestore<<data2<<endl;
}
ifstream fileread("customer.dat");//read data from file
string info1;
int info2;
while(fileread>>info1>>info2){
cout<<info1<<" "<<info2<<endl;
}
return 0;
}

How to filestream strings(including spaces)?

I have an existing text file called database.txt. If I try to add a string at the end that contains a space, it only adds the string content until the space. How can I add the whole string input, including the spaces?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string line;
fstream database;
database.open("Database.txt",ios::app);
cout<<"Name";
cin>>line;
database<<line;
database.close();
return 0;
}
Instead of using std::cin >> line; try use std::getline(std::cin,line);

fstream for reading and writing

In test.txt file i have one word "aa". I want to replace it with "aa1". However, the program below does not change the file. What's wrong?
#include <string>
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream iofile("test.txt",ios_base::in|ios_base::app);
if (!iofile)
cerr << "Unable to open file!";
string word;
iofile >> word;
word.push_back('1');
iofile.seekg(0);
iofile << word;
}
You realize that ios_base::app is causing you to append to the end of the file no matter where you try to seek for write, right? Maybe you meant to specific ios_base::out instead?
Also, for writes, it's seekp() not seekg().