473,804 Members | 3,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reversing a file

neo
Hello everyone,
I want to write a function that takes a simple text file as input and
outputs a file such that the last line in the input file becomes the first
line in the output file and the first line of the input becomes the last
line of output.

I had written this function as:

void fileReverse(cha r* inputFileName, char *outputFileName )
{
FILE *input = fopen(inputFile Name, "r");
FILE *output = fopen(outputFil eName, "w");

int ch, prev = '\n';
while ( (ch = fgetc(input)) != EOF ) /* Read all chars in the file. */
{
if ( ch == '\n' )
{
fputc(EOF, output);
fseek(output, 0, SEEK_SET);
// rewind(output);
fputc(ch, output);
fseek(output, 0, SEEK_SET);
// rewind(output);
}
else
{
fputc(ch, output);
}
prev = ch; /* Keep a copy to later test whether... */
}

if ( prev != '\n' ) /* ...the last line did not end in a newline. */
{
fputc('\n', output);
}

fclose(output);
fclose(input);
}

But on giving input file input.txt as:
line 1
line 2
line 3

it gives output file output.txt as:
line 3

Can someone suggest me a solution?

Regards,
neo.
Jul 7 '06 #1
6 1876
TB
neo skrev:
Hello everyone,
I want to write a function that takes a simple text file as input and
outputs a file such that the last line in the input file becomes the first
line in the output file and the first line of the input becomes the last
line of output.

I had written this function as:
<code snipped>
>
But on giving input file input.txt as:
line 1
line 2
line 3

it gives output file output.txt as:
line 3

Can someone suggest me a solution?
#include <fstream>
#include <string>
#include <vector>
#include <algorithms>

int main(int argc, char* argv[])
{
std::ifstream in("in.txt");
if(in) {
std::vector<std ::stringv;
while(!in.eof() && in) {
std::string str;
std::getline(in ,str,'\n');
v.push_back(str );
}
std::ofstream out("out.txt");
if(out) {
std::copy(v.rbe gin(),v.rend(),
std::ostream_it erator<std::str ing>(out,"\n")) ;
}
}
return 0;
}
--
TB @ SWEDEN
Jul 7 '06 #2

"TB" <TB@SWEDENwro te in message
news:44******** *************** @news-taz.teranews.co m...
#include <fstream>
#include <string>
#include <vector>
#include <algorithms>
#include <algorithm>

Jul 7 '06 #3

TB wrote:
neo skrev:
Hello everyone,
I want to write a function that takes a simple text file as input and
outputs a file such that the last line in the input file becomes the first
line in the output file and the first line of the input becomes the last
line of output.

I had written this function as:

<code snipped>

But on giving input file input.txt as:
line 1
line 2
line 3

it gives output file output.txt as:
line 3

Can someone suggest me a solution?

#include <fstream>
#include <string>
#include <vector>
#include <algorithms>

int main(int argc, char* argv[])
{
std::ifstream in("in.txt");
if(in) {
std::vector<std ::stringv;
while(!in.eof() && in) {
std::string str;
std::getline(in ,str,'\n');
v.push_back(str );
}
std::ofstream out("out.txt");
if(out) {
std::copy(v.rbe gin(),v.rend(),
std::ostream_it erator<std::str ing>(out,"\n")) ;
}
}
return 0;
}
--
TB @ SWEDEN
And if it is a huge file, better split your output to multiple files
(clearing the vector after every iteration) and in the end, just append
those files in reverse order.

Jul 7 '06 #4
neo
Thanks for the solution but the thing is i need to write it in C, not C++.
-neo

"TB" <TB@SWEDENwro te in message
news:44******** *************** @news-taz.teranews.co m...
neo skrev:
>Hello everyone,
I want to write a function that takes a simple text file as input and
outputs a file such that the last line in the input file becomes the
first line in the output file and the first line of the input becomes the
last line of output.

I had written this function as:

<code snipped>
>>
But on giving input file input.txt as:
line 1
line 2
line 3

it gives output file output.txt as:
line 3

Can someone suggest me a solution?

#include <fstream>
#include <string>
#include <vector>
#include <algorithms>

int main(int argc, char* argv[])
{
std::ifstream in("in.txt");
if(in) {
std::vector<std ::stringv;
while(!in.eof() && in) {
std::string str;
std::getline(in ,str,'\n');
v.push_back(str );
}
std::ofstream out("out.txt");
if(out) {
std::copy(v.rbe gin(),v.rend(),
std::ostream_it erator<std::str ing>(out,"\n")) ;
}
}
return 0;
}
--
TB @ SWEDEN

Jul 7 '06 #5
Plese don't top-post.

neo wrote:
Thanks for the solution but the thing is i need to write it in C, not C++.
Why are you asking in a C++ newsgroup then instead of a C newsgroup?

Jul 7 '06 #6
"TB" <TB@SWEDENwro te in message
news:44******** *************** @news-taz.teranews.co m...
>>#include <fstream>
#include <string>
#include <vector>
#include <algorithms>


#include <algorithm>
I tried using the code in Borland Builder 6.0 and I get the error:
'ostream_iterat or' is not a member of 'std'.
Is it included in other file
PZ
Jul 7 '06 #7

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

Similar topics

4
5977
by: Kevin | last post by:
Hello, I need to some help in reversing an 2-dimensional array. I am working with gif images and I am trying to make the mirror image. I was hoping that someone could help give me a headstart in how I can accomplish this. Also, I don't know the the size of the array before hand as the image can be any size. I already have the my read and write gif functions working, but I just need to know how to reverse the contents.
11
8096
by: Tim Marshall | last post by:
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users to pick colours for various control properties in certain apps. Is there a way to take the colour code that is displayed in a backcolor/forecolor/etc property and calculate the "reverse colour"? In other words, If a user picks 255 (red) for a control backcolor, I'd like to be able to calculate the opposite or negative of that colour and assign the control's...
45
5236
by: Ajay | last post by:
Hi all,can you please tell the most efficient method to reverse a byte.Function should return a byte that is reversed.
3
9489
by: dru | last post by:
Problem: Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array. Given an array a , an int variable n containing the number of elements in a , and two other int variables, k and temp , write a loop that reverses the elements of the array. Do not use any other variables besides ...
4
2714
by: hello12 | last post by:
Hello, I am new to java and i was having a hard time figuring out on how to do certain string manipulations. I was asked to read in a text file and reverse the words. So far, I have put all the words in the text file into an arraylist. For example... Hello World. ---> must be printed out as --> olleH .dlroW right now when i print my arraylist i am getting I was thinking of somehow...
8
4764
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
16
2095
by: Scott | last post by:
Yeah I know strings == immutable, but question 1 in section 7.14 of "How to think like a computer Scientist" has me trying to reverse one. I've come up with two things, one works almost like it should except that every traversal thru the string I've gotten it to repeat the "list" again. This is what it looks like: for char in x: mylist.append(char)
7
5106
by: benn686 | last post by:
Anyone know of an efficient way of reversing the bits of a word??
8
1671
by: gearoid | last post by:
Hey I've designed an intranet site where users can upload documents (called 'resources') as part of a knowledge base. Part of the functionality of said site is that privilaged users can edit the documents in the system. This involves uploading a new file for a 'resource'. However, after a user has performed their editing, they are asked to confirm their changes (in a page called confirm.php). When confirm.php loads, the new file upload is...
0
9705
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
9575
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
10564
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...
0
10320
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...
0
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6846
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
5513
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
4288
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
2981
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.