Reading a string from file until whitespace c++

Click For Summary
SUMMARY

This discussion focuses on reading strings from a file in C++ until whitespace is encountered, appending specific symbols to the strings, and storing them in a buffer. The user is guided to utilize the std::ifstream class for file reading and std::stringstream for buffering the output. The provided code snippets demonstrate how to read from a file and manipulate strings effectively, ensuring that each string is followed by a "$" and "(", as specified in the user's requirements.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with file handling using std::ifstream
  • Knowledge of string manipulation in C++
  • Experience with using std::stringstream for buffering
NEXT STEPS
  • Learn about C++ file I/O operations using std::ifstream and std::ofstream
  • Explore string manipulation techniques in C++ for advanced formatting
  • Research the use of loops and conditionals in C++ for processing file data
  • Investigate error handling in file operations to ensure robust code
USEFUL FOR

Beginner C++ programmers, software developers working with file I/O, and anyone looking to enhance their string processing skills in C++.

nicolegrace
Messages
5
Reaction score
0
I am newbie to programming , I am trying to write a program in c++ to read strings from the file until white space [ space/ newline] each time i encounter white space i append to the string read append a symbol "$" and push to the buffer. and for the same string append another symbol "(" and push this again to buffer.

example :

say I have file as following :example.txt

cat dog monkey
tree daimond

So it should read as following in the buffer

Buf[] = cat$cat(dog$dog(monkey$monkey(tree$tree(daimond$daimond(


can someone help me c++ code for this . thank you !
 
Technology news on Phys.org
You can use an object of type std::ifstream to read from the text file. If you use the operator ">>" to read from the file, where the std::ifstream is to the left of the ">>" and an std::string is to the right, you can read strings in a manner like you described.

Code:
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string s;
    ifstream file("example.txt");
    file >> s;
    return 0;
}

For this program, s would contain "cat" if example.txt was like you described.

You can have this done in a loop to read from the file multiple times.

You can check the [boolean] value of the ifstream itself to see if there is more to read.
Here is a way to read the entire file:

Code:
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string s;
    ifstream file("example.txt");
    while(file >> s)
    {
        //do stuff
    }
    return 0;
}

You can use an object of type std::stringstream to write to a buffer.

Code:
#include <sstream>
#include <iostream>
using namespace std;

int main()
{
    stringstream ss;
    string s;
    ss << "testing ";
    ss << '$'; 
    ss << 12345;

    s = ss.str();
    cout << s << endl;

    return 0;
}

For this program, the console would show
testing $12345

ss is effectively a buffer, but I showed how you can copy its contents into the std::string s, since an std::string may be more like the buffers with which you may be familiar.
 
Last edited:

Similar threads

  • · Replies 8 ·
Replies
8
Views
5K
Replies
10
Views
5K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 11 ·
Replies
11
Views
35K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 10 ·
Replies
10
Views
6K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 19 ·
Replies
19
Views
4K
  • · Replies 13 ·
Replies
13
Views
2K