text_data = {apple \n
             grapes \n
             straberry};
FILE *fp; int i;        char ch;
std::string DataVal;

std::ifstream in("D:\\test\\file.txt", std::ios::in | std::ios::binary);

    if(NULL != in)
    {       cout<<"File Open Success"<<endl;
    in.seekg(0, std::ios::end);
    DataVal.resize(in.tellg());
    in.seekg(0, std::ios::beg);
    in.read(&DataVal[0], DataVal.size());
    in.close();
    }
    else {  cout<<"File Open Failure"<<endl;
            return 0;                               
    }
  • Here I am able to copy the entire file content into the "DataVal". That is fine.
    Instead I want to copy the text data into DataVal.at(i++); whenever it hits the new line.

    Thanks well in advance.
    Praveen*

Make a vector of string.
Use std::getline to read in one line at a time, and push_back each line onto the vector.

Thank you very much.
I have tried as suggested. it worked. :-)

Due to some constraints in the project I am not able to use "ifstream".
Could you please suggest some other ways, if possible (both C/C++ example).
Way to read the file content line by line and copy it into the vector of strings.

Thanks well in Advance
Praveen

Why are you not able to use ifstream? If this is c++ code then either fstream or ifstream is what you need to use to read data from a file. Is this supposed to be C code?

Code is in C++ (Company Restriction). Little progress at my side.
Now the requirement is I need to read the file content directly into a std::string. If there is code snippet, Please Share.

Once we have the file data read into std::string, further I can use the library function supplied from the company to store.

please share the code snippet.
Thanks,
Praveen

What about this :

#include <fstream>
#include <string>

int main()
{
    std::ifstream m_File("/home/user/file_name.txt", std::ios::in);

    std::filebuf* pbuf = m_File.rdbuf();

    std::size_t size = pbuf->pubseekoff(0, m_File.end, m_File.in);
    pbuf->pubseekpos(0, m_File.in);

    char* buffer = new char[size];

    pbuf->sgetn(buffer, size);

    std::string text(buffer);

    return 0;
}

Thank you very much JBat.

One more Linker issue.
for the "std::vector<std::string> vData;"
I am getting the linker error, Multiple definitions of "vData".
It seems like the vector of string has been defined in other files and during the time the linker tried to link, found multiple definetions.

How to get rid of this linker error. Kindly suggest.

Thanks,
Praveen

Show your source code.

I have seen in other header files where "std::vector<std::string>v_xxx" has been declared. Thats is reason for the linker error.
Object file has been created for my source file, But while linking those object files linker found multiple declarations, & thus the error
error, Multiple definitions of "vData".

the Error is for the mentioned declaration.
How to get rid of it ?
Sorry, cannot show the source code. Seems its the error pertainig to the multiple declarations.

you have vData included in multiple files so you are getting a multiple redefinition error. You can only have 1 vData in the global scope

vData is one vector object that is defined once only. Whereas I have seen multiple data type declaration for "std::vector<std::string> x_bla" in different class, leads to this problem.

Thanks everybody for the responses. Its the human error.
My teamate has changed the scope that caused the problem.
Everything Resolved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.