473,748 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read line after line but from the eof.

Hello.
How could I read the whole text file line after line from the end of
file? (I want to ~copy~ file)

I want to copy file in this way:

file 1:

a
b
c
file 2:

c
b
a
--
Ground21_______ _____\\
ground21@poczta KROPKAfm
Dec 22 '06 #1
24 3216
Ground21 wrote:
Hello.
How could I read the whole text file line after line from the end of
file? (I want to ~copy~ file)

I want to copy file in this way:

file 1:

a
b
c
file 2:

c
b
a
The easiest way would be to read all the lines into something like an
std::vector<std ::string, then write the new file iterating backwards
through the vector. As long as you don't have huge files, this should work
ok.

Dec 22 '06 #2
The easiest way would be to read all the lines into something like an
std::vector<std ::string, then write the new file iterating backwards
through the vector. As long as you don't have huge files, this should work
ok.
I think I wrote bad example:

Once more:

file1:

text1
text2
text3
abctest

file2:

abctest
text3
text2
text1
--
Ground21_______ _____\\
ground21@poczta KROPKAfm
Dec 22 '06 #3
Ground21 wrote:
>
>The easiest way would be to read all the lines into something like an
std::vector<st d::string, then write the new file iterating backwards
through the vector. As long as you don't have huge files, this should
work ok.

I think I wrote bad example:
I did understand your posting anyway.
Once more:

file1:

text1
text2
text3
abctest

file2:

abctest
text3
text2
text1
Yup. What you should do is read the file line by line and put each line as a
separate string into a vector. So later you have a vector of lines. Then
you can iterate through that vector backwards and write the strings out
again.

Dec 22 '06 #4

Rolf Magnus napsal:
Ground21 wrote:
The easiest way would be to read all the lines into something like an
std::vector<std ::string, then write the new file iterating backwards
through the vector. As long as you don't have huge files, this should
work ok.
I think I wrote bad example:

I did understand your posting anyway.
Once more:

file1:

text1
text2
text3
abctest

file2:

abctest
text3
text2
text1

Yup. What you should do is read the file line by line and put each line as a
separate string into a vector. So later you have a vector of lines. Then
you can iterate through that vector backwards and write the strings out
again.
I would recommend std::list instead of std::vector, because list can be
simplier (== more effectively) extended in length.

Dec 22 '06 #5
Ondra Holub wrote:
Rolf Magnus napsal:
>Ground21 wrote:
>>>The easiest way would be to read all the lines into something like an
std::vector< std::string, then write the new file iterating backwards
through the vector. As long as you don't have huge files, this should
work ok.
I think I wrote bad example:
I did understand your posting anyway.
>>Once more:

file1:

text1
text2
text3
abctest

file2:

abctest
text3
text2
text1
Yup. What you should do is read the file line by line and put each line as a
separate string into a vector. So later you have a vector of lines. Then
you can iterate through that vector backwards and write the strings out
again.

I would recommend std::list instead of std::vector, because list can be
simplier (== more effectively) extended in length.
I used deque.

I tried to be real clever, with reverse_copy on an istream_iterato r, and
an ostream_iterato r, but reverse_copy requires Bidirectional Iterators. :(
Dec 22 '06 #6
Rolf Magnus napisał(a):
Yup. What you should do is read the file line by line and put each line as a
separate string into a vector. So later you have a vector of lines. Then
you can iterate through that vector backwards and write the strings out
again.
can You tell me what's wrong with this code?:
(I want to save lines from file 'file' to vectors, and then, write them
into "tmp.txt"). I got wrong output "tmp.log".. . I think that is problem
with char*...

#include <vector.h>
vector <char*tab(10) ;

tmp=fopen("tmp. txt","w");
fseek(file, 0L, SEEK_SET);
for(int i=0; i<lines; i++)
{
fgets(tab[i],256,file);
}
for(int i=lines; i>0; i--)
{
fputs(tab[i],tmp);
}
--
Ground21_______ _____\\
ground21@poczta KROPKAfm
Dec 22 '06 #7
Ground21 wrote:
Rolf Magnus napisał(a):
>Yup. What you should do is read the file line by line and put each
line as a
separate string into a vector. So later you have a vector of lines. Then
you can iterate through that vector backwards and write the strings out
again.

can You tell me what's wrong with this code?:
(I want to save lines from file 'file' to vectors, and then, write them
into "tmp.txt"). I got wrong output "tmp.log".. . I think that is problem
with char*...

#include <vector.h>
vector <char*tab(10) ;

tmp=fopen("tmp. txt","w");
fseek(file, 0L, SEEK_SET);
for(int i=0; i<lines; i++)
{
fgets(tab[i],256,file);
}
for(int i=lines; i>0; i--)
{
fputs(tab[i],tmp);
}

Lets see...

1. What is "lines"?
2. What happens if there are more than 10 lines in a file?
3. What happens if a line is bigger than 256?
4. Why are you using arrays and C-style I/O?
5. What is <vector.h>? The C++ header is <vector>
6. Every entry in your vector is identical, since you're storing the
same address over and over.

And that's just off the top of my head.

Dec 22 '06 #8
red floyd napisał(a):
1. What is "lines"?
int lines=0;
fseek(fileh,0L, SEEK_SET);
while(feof(file h)==0) if(fgetc(fileh) =='\n') linies++;

lines - number of lines in my text file.
2. What happens if there are more than 10 lines in a file?
I read in wikibooks, that value of size will automaticlly increase if
necessary...

3. What happens if a line is bigger than 256?
I know, that it can be done better using something like strlen to check
line lenght, but for me, 256 is OK.
4. Why are you using arrays and C-style I/O?
5. What is <vector.h>? The C++ header is <vector>
in c++ builder 6 it works the same for <vectorand <vector.h>...
6. Every entry in your vector is identical, since you're storing the
same address over and over.

I though, that I take first line to tab[0], second line to tab[1]...

--
Ground21_______ _____\\
ground21@poczta KROPKAfm
Dec 22 '06 #9
r
Ground21 wrote:
>
can You tell me what's wrong with this code?:
(I want to save lines from file 'file' to vectors, and then, write them
into "tmp.txt"). I got wrong output "tmp.log".. . I think that is problem
with char*...

#include <vector.h>
vector <char*tab(10) ;

tmp=fopen("tmp. txt","w");
fseek(file, 0L, SEEK_SET);
for(int i=0; i<lines; i++)
{
fgets(tab[i],256,file);
}
for(int i=lines; i>0; i--)
{
fputs(tab[i],tmp);
}
It's a bad mix of C and C++, besides the "Access violation".

// reading a file line by line
std::ifstream ifs ("tmp.txt");
std::string line;
std::vector<std ::stringlines;
while (getline (ifs, line)) {
lines.push_back (line);
}

Is this already in the C++ FAQ?

Dec 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
8796
by: Chuck Anderson | last post by:
Can anyone point me in the right direction? I want to use Php to automate confirmation of someone joining an email list by them replying to an email (so they don't have to have a browser?). I will probably use a hyperlink with a unique ID, but I also want to know how to go about reading from a mailbox with Php so I can use an email reply method, too. I'm having trouble finding any kind of tutorial. From the bit of searching I've done,...
2
22823
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F: operator delete(void*) (vg_replace_malloc.c:156) ==11604== by 0x804A1BA: __gnu_cxx::new_allocator<Foo>::deallocate(Foo*, unsigned) (new_allocator.h:86) ==11604== by 0x8049C08: std::_Vector_base<Foo, std::allocator<Foo> >::_M_deallocate(Foo*,...
18
4893
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
4
2745
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void Main()
4
9472
by: oncelovecoffee | last post by:
str_Array() 'strings I need save it to file and next time i can read from file. -------------------------------------------------------- -------------------------------------------------------- Save Function I use str_FromAndToFile=join(str_Array,"VBTAB") -------------------------------------------------------- Read Function
6
6001
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to assign a value to them. I wanted to trap for this exception in a subclass using super but it doesn't happen. I have read Guido's tutorial on new style classes and Shalabh's tuturial on new style attributes and methods, and thought I understood...
11
3350
by: waffle.horn | last post by:
Hi, if this makes sense i want to create a function that can be called so that it reads a single line from a file, then after using the information destroys it. Such that when the function is called that line of info does not exist anymore in the file. for example i have created a short example of where i am at, but I dont know how to alter what im doing so that i just read in a single line
9
7385
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying to read was maindata.xml . I cannot however seem to use the gzip module correctly. Have tried the program 2 ways for no success, any ideas would be appreciated. Attempt 1 #!/usr/bin/python
6
5716
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
6
3735
by: Sean Davis | last post by:
I have a large file that I would like to transform and then feed to a function (psycopg2 copy_from) that expects a file-like object (needs read and readline methods). I have a class like so: class GeneInfo(): def __init__(self): #urllib.urlretrieve('ftp://ftp.ncbi.nih.gov/gene/DATA/ gene_info.gz',"/tmp/gene_info.gz")
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.