Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,459 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,802 people online right now. Registration is fast and FREE... Join Now!




Reading strings from file

 
Reply to this topicStart new topic

Reading strings from file

Elegy
8 Aug, 2008 - 04:56 AM
Post #1

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 4


My Contributions
there is a text in file, made of strings. I need to delete unnecessary spaces from every string (in the beginning, in the end of the string and between the words should leave only one). Result must be written in another file.

I've tried to do that different ways, but I always come out with the same output: everything is written in one line, even if input file is written in two or more

it seems like programm gets everything in one stream
how can i do it different way?

CODE

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    string stroka;
    ifstream in;
    ofstream out;

in.open("c:\\borland\\my files\\files for reading\\input.txt");    
out.open("c:\\borland\\my files\\files for reading\\output.txt");

if((!in)||(!out))
{
cerr<<"Unable to open file.";
exit (1);    
    }
while (in>>stroka)
    out<<stroka<<' ';
return 0;
}


There is another way here

CODE

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int i;
    string stroka[10];
    ifstream in;
    ofstream out;

in.open("c:\\borland\\my files\\files for reading\\input.txt");    
out.open("c:\\borland\\my files\\files for reading\\output.txt");

if((!in)||(!out))
{
cerr<<"Unable to open file.";
exit (1);    
    }
for (i=0;i<10;i++)
{in>>stroka[i];
out<<stroka[i]<<' ';}
return 0;
}

User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Reading Strings From File
8 Aug, 2008 - 05:14 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
The simplest way to do it would be to read all of the contents into a string. Then, remove multiple spaces. You could do that with my function, found here.

Now we need to write back to the file. The simplest way to do that would be to create an ofstream, using ios::trunc to replace the current file. Then, write the string into that file.

Hope this helps smile.gif

Edit:
For more information on file streams, click here
User is offlineProfile CardPM
+Quote Post

net nerd865
RE: Reading Strings From File
8 Aug, 2008 - 07:47 AM
Post #3

New D.I.C Head
*

Joined: 16 Jun, 2008
Posts: 44



Thanked: 2 times
My Contributions
I would suggest something like this:

CODE

ifstream fin;

vector<string> stuff;

while(!fin.eof())
{
stuff.push_back(fin.getline());
}


Something like that should get all the strings out of the file, then just to a for loop to put them in a new file.

As for removing the spaces, there are a few string manip. functions you could use. I'm not sure what they are off the top of my head, but if you are familiar with the STL, try doing that.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 04:05PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month