Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > setting read and write point

Reply
Thread Tools

setting read and write point

 
 
John Flynn
Guest
Posts: n/a
 
      08-26-2004
hi,

having problems reading from and writing back to the same file.

basically, i want to read lines of text from a file and reverse them and
write them back to the same file.. it has to replace the text its reversing


eg.

the quick brown fox
jumped over the
lazy dog

.....becomes..

xof nworb kciuq eht
eht revo depmuj
god yzal


following is what i've got so far.. its all over the place.. for me its a
lot of trial and error.. hence all the code that i've made into comments
(//)
any help would be greatly appreciated

************************************************** ******************

#include <iostream>
#include <fstream>

using namespace std;



/***********************
* Main execution
***********************/
int main()//int argc, char* argv[])
{
// if (argc != 2)
{
// cout << "\nUsage: rever file\n\n";
// return 1;
}

// string fileName(argv[1]);
string readLine;
string tmp;
fstream stream;
fstream outStream;
long readPos, writePos;
stream.open("fileName.txt");
//outStream.open("fileName.txt");
//outStream.open("fileName.txt");

if (!stream.is_open())
{
cout << "Unable to open: " << "fileName" << endl;
//return 1;
}
readPos = 0;
writePos = 0;
for (;
{

// writePos = stream.tellp();

//stream.seekg(readPos, ios::beg);
getline(stream, readLine);

readPos = stream.tellg();

for (int i = readLine.length() - 1; i >= 0; i-- )
{
tmp = tmp + readLine[i];
}

// stream.seekp(writePos, ios::beg);

// outStream << readPos << endl;



cout << readPos << "and "<< writePos;
cin >> tmp;


if (stream.eof()) break;
tmp = "";
}




}


 
Reply With Quote
 
 
 
 
Jacques Labuschagne
Guest
Posts: n/a
 
      08-26-2004
John Flynn wrote:

> hi,
>
> having problems reading from and writing back to the same file.
>
> basically, i want to read lines of text from a file and reverse them and
> write them back to the same file.. it has to replace the text its reversing
>
>
> eg.
>
> the quick brown fox
> jumped over the
> lazy dog
>
> ....becomes..
>
> xof nworb kciuq eht
> eht revo depmuj
> god yzal
>
>
> following is what i've got so far.. its all over the place.. for me its a
> lot of trial and error.. hence all the code that i've made into comments
> (//)
> any help would be greatly appreciated



Far too complex. Why not read it all in and then write it all out in
reverse order?

#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>

int main(){
using namespace std;
istreambuf_iterator<char> first(cin), last;
vector<char> v(first, last);
copy(v.rbegin(), v.rend(), ostreambuf_iterator<char>(cout));
}


Jacques.
 
Reply With Quote
 
 
 
 
Sam Holden
Guest
Posts: n/a
 
      08-26-2004
On Thu, 26 Aug 2004 21:28:41 +1200,
Jacques Labuschagne <(E-Mail Removed)> wrote:
> John Flynn wrote:
>
>> hi,
>>
>> having problems reading from and writing back to the same file.
>>
>> basically, i want to read lines of text from a file and reverse them and
>> write them back to the same file.. it has to replace the text its reversing
>>
>>
>> eg.
>>
>> the quick brown fox
>> jumped over the
>> lazy dog
>>
>> ....becomes..
>>
>> xof nworb kciuq eht
>> eht revo depmuj
>> god yzal
>>
>>
>> following is what i've got so far.. its all over the place.. for me its a
>> lot of trial and error.. hence all the code that i've made into comments
>> (//)
>> any help would be greatly appreciated

>
>
> Far too complex. Why not read it all in and then write it all out in
> reverse order?


Because that results in output completely unrelated to the specified
required output?

And ignores the "same file" requirement that seems rather important?


> #include <vector>
> #include <iostream>
> #include <iterator>
> #include <algorithm>
>
> int main(){
> using namespace std;
> istreambuf_iterator<char> first(cin), last;
> vector<char> v(first, last);
> copy(v.rbegin(), v.rend(), ostreambuf_iterator<char>(cout));
> }


#include <cstdio>
#include <stack>

using namespace std;

void in_place_line_reverse(FILE *file) {
stack<int> line;
int c;
fpos_t start_of_line;
fpos_t end_of_line;

fgetpos(file, &start_of_line);
while ((c=getc(file)) != EOF) {
if (c=='\n') {
fgetpos(file, &end_of_line);
fsetpos(file, &start_of_line);
while (line.size() > 0) {
putc(line.top(), file);
line.pop();
}
fsetpos(file, &end_of_line);
fgetpos(file, &start_of_line);
} else {
line.push(c);
}
}
}

int main() {
FILE *file;
file = fopen("text.txt", "r+");
in_place_line_reverse(file);
}


But it's more C than C++

And needs to double in length in order to actually perform
error checking on all those functions that can fail


--
Sam Holden
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-26-2004
Sam Holden wrote:

> On Thu, 26 Aug 2004 21:28:41 +1200,
> Jacques Labuschagne <(E-Mail Removed)> wrote:
>> John Flynn wrote:
>>
>>> hi,
>>>
>>> having problems reading from and writing back to the same file.
>>>
>>> basically, i want to read lines of text from a file and reverse them and
>>> write them back to the same file.. it has to replace the text its
>>> reversing
>>>
>>>
>>> eg.
>>>
>>> the quick brown fox
>>> jumped over the
>>> lazy dog
>>>
>>> ....becomes..
>>>
>>> xof nworb kciuq eht
>>> eht revo depmuj
>>> god yzal
>>>
>>>
>>> following is what i've got so far.. its all over the place.. for me its
>>> a lot of trial and error.. hence all the code that i've made into
>>> comments (//)
>>> any help would be greatly appreciated

>>
>>
>> Far too complex. Why not read it all in and then write it all out in
>> reverse order?

>
> Because that results in output completely unrelated to the specified
> required output?
>
> And ignores the "same file" requirement that seems rather important?
>
>
>> #include <vector>
>> #include <iostream>
>> #include <iterator>
>> #include <algorithm>
>>
>> int main(){
>> using namespace std;
>> istreambuf_iterator<char> first(cin), last;
>> vector<char> v(first, last);
>> copy(v.rbegin(), v.rend(), ostreambuf_iterator<char>(cout));
>> }

>
> #include <cstdio>
> #include <stack>
>
> using namespace std;
>
> void in_place_line_reverse(FILE *file) {
> stack<int> line;
> int c;
> fpos_t start_of_line;
> fpos_t end_of_line;
>
> fgetpos(file, &start_of_line);
> while ((c=getc(file)) != EOF) {
> if (c=='\n') {
> fgetpos(file, &end_of_line);
> fsetpos(file, &start_of_line);
> while (line.size() > 0) {
> putc(line.top(), file);
> line.pop();
> }
> fsetpos(file, &end_of_line);
> fgetpos(file, &start_of_line);
> } else {
> line.push(c);
> }
> }
> }
>
> int main() {
> FILE *file;
> file = fopen("text.txt", "r+");
> in_place_line_reverse(file);
> }
>
>
> But it's more C than C++
>
> And needs to double in length in order to actually perform
> error checking on all those functions that can fail
>
>


What about:

#include <vector>
#include <string>
#include <fstream>
#include <iterator>
#include <algorithm>

typedef std::string Line;
typedef std::vector<Line> Text;

int main( int argn, char ** args ){
Text text;
{ // read:
std::fstream in_file ( args[1] );
Line line;
while ( std::getline( in_file, line ) ) {
text.push_back( line );
}
in_file.close();
}
{ // write:
std::fstream out_file ( args[1] );
std:streambuf_iterator<char> out_iter ( out_file );
for ( Text::const_iterator line_iter = text.begin();
line_iter != text.end(); ++ line_iter ) {
std::copy(line_iter->rbegin(), line_iter->rend(), out_iter );
*out_iter = '\n';
++ out_iter;
}
}
}


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
Scenario 5: IS-IS routing on Frame Relay Multi-point and Point-to-Point David Sudjiman Cisco 0 06-08-2006 09:11 AM
setting speed limit on point-to-point t1? artc Cisco 0 05-14-2006 05:20 PM



Advertisments