Reading a string from file until whitespace c++

In summary, the conversation is about writing a program in c++ to read strings from a file and append symbols to each string before pushing them to a buffer. The program uses objects of type std::ifstream and std::stringstream to read from the file and write to the buffer, respectively. A loop is used to read multiple strings from the file.
  • #1
nicolegrace
5
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
  • #2
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:

Related to Reading a string from file until whitespace c++

1. How do I read a string from a file in C++?

To read a string from a file in C++, you can use the getline() function from the fstream library. This function takes in two parameters - the file stream and the string variable where you want to store the input. It will read the entire line from the file until it reaches a newline character or the end of the file.

2. How do I read a string from a file until whitespace?

To read a string from a file until whitespace in C++, you can use the getline() function with the optional third parameter, which specifies the delimiter. You can set the delimiter to be a whitespace character, such as a space or a tab, which will make the function stop reading when it reaches that character.

3. What happens if there is no whitespace in the string I am trying to read?

If there is no whitespace in the string you are trying to read, the getline() function will read the entire line from the file and store it in your string variable. It will not stop until it reaches the end of the line or the end of the file.

4. How can I check if the file I am reading from is empty?

You can use the eof() function from the fstream library to check if the end of the file has been reached. If the file is empty, this function will return true. You can use this as a condition in a while loop to read from the file until the end is reached.

5. Is it necessary to close the file after reading from it?

Yes, it is good practice to close the file after reading from it. This ensures that all data is written to the file and any resources used by the file are released. You can use the close() function from the fstream library to close the file stream.

Similar threads

  • Programming and Computer Science
Replies
1
Views
371
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
10
Views
5K
  • Programming and Computer Science
Replies
5
Views
6K
  • Programming and Computer Science
Replies
11
Views
34K
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
10
Views
5K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top