473,763 Members | 6,772 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
24 3220
Ground21 wrote:
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...
Yes, if you use the push_back member function.
>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.
It would be better as well as easier to use std::string instead of raw char
arrays and pointers.
>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>...
Well, then you better stick to the standard header <vector>, otherwise your
program will be non-portable.
>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]...
You could try something based on this (untested):

#include <istream>
#include <ostream>
#include <string>
#include <vector>

void reverse_copy_fi le(std::istream & infile, std::ostream& outfile)
{
typedef std::vector<std ::string vec_type;
vec_type text;

std::string line;
while (std::getline(i nfile, line))
text.push_back( line);

vec_type::const _reverse_iterat or it = text.rbegin(), end = text.rend();
for (; it != end; ++it)
outfile << line << '\n';
}
Dec 22 '06 #11
r wrote:
// 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?
[C++ Error] zapis.cpp(136): E2285 Could not find a match for
'getline<_CharT ,_Traits,_Alloc >(FILE *,string)'

--
Ground21_______ _____\\
ground21@poczta KROPKAfm
Dec 22 '06 #12
Ground21 wrote:
r wrote:
>// reading a file line by line
std::ifstrea m ifs ("tmp.txt");
std::string line;
std::vector<st d::stringlines;
while (getline (ifs, line)) {
lines.push_back (line);
}

Is this already in the C++ FAQ?

[C++ Error] zapis.cpp(136): E2285 Could not find a match for
'getline<_CharT ,_Traits,_Alloc >(FILE *,string)'
That's because you didn't copy the whole example. He used an ifstream
for input, not a FILE*.

Use C++ style I/O if you're writing C++.
Dec 23 '06 #13

Ground21 wrote in message ...
>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>
#include <vector// C++
>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);
}

#include <vector// C++
#include <string>
#include <fstream>
#include <algorithm>

int main(){
std::ifstream copin( "ZZtest.txt " );
if( not copin.is_open() ){ throw "a fit";}

std::vector<std ::stringTmpFile ;
for( std::string line; std::getline( copin, line ); ){
TmpFile.push_ba ck(line);
}
// copin.close();
std::reverse( TmpFile.begin() , TmpFile.end() ); // reverse it
std::ofstream out( "ZZtestR.tx t" );
for( size_t i(0); i < TmpFile.size(); ++i ){
std::reverse( TmpFile.at(i).b egin(), TmpFile.at(i).e nd());
out<< TmpFile.at( i ) <<std::endl;
}
// out.close();
} // main()
// ----------
// - contents "ZZtest.txt " -
Record 0
Record 1
Record 2
Record 3
Record 4
Record 5
Record 6

// - contents "ZZtestR.tx t" -
6 droceR
5 droceR
4 droceR
3 droceR
2 droceR
1 droceR
0 droceR

--
Bob R
POVrookie
Dec 23 '06 #14
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

I think the most simple solution would use a stack an C++ I/O.

#include <iostream>
#include <fstream>
#include <stack>
#include <string>

using namespace std;

int main()
{
ifstream in("in.txt");
ofstream out("out.txt");
stack<stringss;

string s;
in >s;
while (!in.eof()) {
ss.push(s);
in >s;
}
while (!ss.empty()) {
out << ss.top() << endl;
ss.pop();
}
return 0;
}

Dec 23 '06 #15
Tobias Gneist wrote:
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


I think the most simple solution would use a stack an C++ I/O.
Hmm, right. A stack is actually a better choice than a vector.
#include <iostream>
#include <fstream>
#include <stack>
#include <string>

using namespace std;

int main()
{
ifstream in("in.txt");
ofstream out("out.txt");
stack<stringss;

string s;
in >s;
while (!in.eof()) {
ss.push(s);
in >s;
}
Simpler and more correct than those last 5 lines would be:

while (in >s)
ss.push(s);
while (!ss.empty()) {
out << ss.top() << endl;
No need to flush the stream after each line, so '\n' instead of endl is
sufficient.
ss.pop();
}
return 0;
}
Dec 23 '06 #16
I think the most simple solution would use a stack an C++ I/O.
>
#include <iostream>
#include <fstream>
#include <stack>
#include <string>

using namespace std;

int main()
{
ifstream in("in.txt");
ofstream out("out.txt");
stack<stringss;

string s;
in >s;
while (!in.eof()) {
ss.push(s);
in >s;
}
If input likes:

test
122 345 345

output would be:

345
345
122
test

How about this

while (std::getline(i n,s)) {
ss.push(s);
}

output would be:

122 345 345
test
while (!ss.empty()) {
out << ss.top() << endl;
ss.pop();
}
return 0;
}
Dec 23 '06 #17
gongzibi napisał(a):
How about this

while (std::getline(i n,s)) {
ss.push(s);
}

output would be:

122 345 345
test
ifstream in("log.txt");
ofstream out("out.txt");
stack<stringss;

string s;
in >s;

while (std::getline(i n,s)) {
ss.push(s);
}
while (!ss.empty()) {
out << ss.top() << endl;
ss.pop(); }

this code works OK, but...

my log.txt file:

d/t: 2006-12-23/18:52:47 D:\c++\sem1\Pro ject1.exe
d/t: 2006-12-23/18:52:49 D:\c++\sem1\Pro ject1.exe
d/t: 2006-12-23/18:52:50 D:\c++\sem1\Pro ject1.exe
d/t: 2006-12-23/18:52:52 D:\c++\sem1\Pro ject1.exe

and output file:

d/t: 2006-12-23/18:52:52 D:\c++\sem1\Pro ject1.exe
d/t: 2006-12-23/18:52:50 D:\c++\sem1\Pro ject1.exe
d/t: 2006-12-23/18:52:49 D:\c++\sem1\Pro ject1.exe
2006-12-23/18:52:47 D:\c++\sem1\Pro ject1.exe

where is my "d/t: " ?
--
Ground21_______ _____\\
ground21@poczta KROPKAfm
Dec 23 '06 #18

Ground21 wrote in message ...
>
ifstream in("log.txt");
ofstream out("out.txt");
stack<stringss;

string s;
in >s;

while (std::getline(i n,s)) {
ss.push(s);
}
while (!ss.empty()) {
out << ss.top() << endl;
ss.pop(); }

this code works OK, but...

my log.txt file:

d/t: 2006-12-23/18:52:47 D:\c++\sem1\Pro ject1.exe
d/t: 2006-12-23/18:52:49 D:\c++\sem1\Pro ject1.exe
d/t: 2006-12-23/18:52:50 D:\c++\sem1\Pro ject1.exe
d/t: 2006-12-23/18:52:52 D:\c++\sem1\Pro ject1.exe

and output file:

d/t: 2006-12-23/18:52:52 D:\c++\sem1\Pro ject1.exe
d/t: 2006-12-23/18:52:50 D:\c++\sem1\Pro ject1.exe
d/t: 2006-12-23/18:52:49 D:\c++\sem1\Pro ject1.exe
2006-12-23/18:52:47 D:\c++\sem1\Pro ject1.exe

where is my "d/t: " ?
string s;
in >s; // it's in this string you didn't save.
// delete that line.

while (std::getline(i n,s)) { ss.push(s); }

--
Bob R
POVrookie
Dec 23 '06 #19
BobR napisał(a):
>where is my "d/t: " ?

string s;
in >s; // it's in this string you didn't save.
// delete that line.

while (std::getline(i n,s)) { ss.push(s); }
works great!
thanks a lot!

--
Ground21_______ _____\\
ground21@poczta KROPKAfm
Dec 23 '06 #20

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

Similar topics

12
8797
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
22829
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
2747
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
9473
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
6003
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
3353
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
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
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
9822
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
7366
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
6642
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();...
1
3917
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
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.