473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Correcting newlines

Hi,

I have the following code, which is supposed to open a file where 0a (hex)
is used to mark a newline, and change this into 0d0a. But it places two 0d's
before every 0a.
Could somebody tell me what's wrong with this code?

/* newyork.c */
#include <stdio.h>

#define LF 10 /* character to find */
#define CR 13 /* character to place */

int main(int argc, char *argv[])
{ if (argc != 3)
{ printf("newyork <source> <target>");
return 0;
}
FILE *source, *target;
char ch;

source = fopen(argv[1], "r");
target = fopen(argv[2], "w");

while ((ch = fgetc(source)) != EOF)
{ if (ch == LF)
fprintf(target, "%c", CR);
fprintf(target, "%c", ch);
}
fclose(source);
fclose(target);
}
Jul 23 '05 #1
3 4449

Hi,
---------------------------------------------
while ((ch = fgetc(source)) != EOF)
{ if (ch == LF)
fprintf(target, "%c", CR);
fprintf(target, "%c", ch);
}
--------------------------------------------

this part of your code should be like this
while ((ch = fgetc(source)) != EOF)
{ if (ch == LF)
fprintf(target, "%c", CR);
else
fprintf(target, "%c", ch);
}
----------------------------------------
once you debug the code you will understand the difference yourself . I
need not discuss it here.

Thanks & Regards

Ambar

Wondiws wrote:
Hi,

I have the following code, which is supposed to open a file where 0a (hex)
is used to mark a newline, and change this into 0d0a. But it places two 0d's
before every 0a.
Could somebody tell me what's wrong with this code?

/* newyork.c */
#include <stdio.h>

#define LF 10 /* character to find */
#define CR 13 /* character to place */

int main(int argc, char *argv[])
{ if (argc != 3)
{ printf("newyork <source> <target>");
return 0;
}
FILE *source, *target;
char ch;

source = fopen(argv[1], "r");
target = fopen(argv[2], "w");

while ((ch = fgetc(source)) != EOF)
{ if (ch == LF)
fprintf(target, "%c", CR);
fprintf(target, "%c", ch);
}
fclose(source);
fclose(target);
}


Jul 23 '05 #2
Wondiws wrote:
Hi,

I have the following code, which is supposed to open a file where 0a (hex)
is used to mark a newline, and change this into 0d0a. But it places two 0d's
before every 0a.
Could somebody tell me what's wrong with this code?

/* newyork.c */
#include <stdio.h>

#define LF 10 /* character to find */
#define CR 13 /* character to place */

int main(int argc, char *argv[])
{ if (argc != 3)
{ printf("newyork <source> <target>");
return 0;
}
FILE *source, *target;
char ch;

source = fopen(argv[1], "r");
// IF the newline in the file is really just 0x0a,
// then open it in binary mode
source = fopen(argv[1], "rb");

// open the output file in text mode so Windows WILL
// automatically add an 0x0d for every 0x0a written. target = fopen(argv[2], "w");

while ((ch = fgetc(source)) != EOF)
{ if (ch == LF)
fprintf(target, "%c", CR);
fprintf(target, "%c", ch);
}
fclose(source);
fclose(target);
}


if this is on Windows, then this code snip might
help:

// IF the newline in the file is really just 0x0a,
// then open it in binary mode
source = fopen(argv[1], "rb");

// open the output file in text mode so Windows WILL
// automatically add an 0x0d for every 0x0a written.
target = fopen(argv[2], "w");

while ((ch = fgetc(source)) != EOF)
{
// if 'ch' is 0x0a, Windows will automatically
// write 0x0d 0x0a when we write 'ch' because
// 'target' was opened in text mode
fprintf(target, "%c", ch);
}

Larry
Jul 23 '05 #3
Larry I Smith wrote:
Wondiws wrote:
Hi,

I have the following code, which is supposed to open a file where 0a (hex)
is used to mark a newline, and change this into 0d0a. But it places two 0d's
before every 0a.
Could somebody tell me what's wrong with this code?

/* newyork.c */
#include <stdio.h>

#define LF 10 /* character to find */
#define CR 13 /* character to place */

int main(int argc, char *argv[])
{ if (argc != 3)
{ printf("newyork <source> <target>");
return 0;
}
FILE *source, *target;
char ch;

source = fopen(argv[1], "r");


// IF the newline in the file is really just 0x0a,
// then open it in binary mode
source = fopen(argv[1], "rb");

// open the output file in text mode so Windows WILL
// automatically add an 0x0d for every 0x0a written.
target = fopen(argv[2], "w");

while ((ch = fgetc(source)) != EOF)
{ if (ch == LF)
fprintf(target, "%c", CR);
fprintf(target, "%c", ch);
}
fclose(source);
fclose(target);
}


if this is on Windows, then this code snip might
help:

// IF the newline in the file is really just 0x0a,
// then open it in binary mode
source = fopen(argv[1], "rb");

// open the output file in text mode so Windows WILL
// automatically add an 0x0d for every 0x0a written.
target = fopen(argv[2], "w");

while ((ch = fgetc(source)) != EOF)
{
// if 'ch' is 0x0a, Windows will automatically
// write 0x0d 0x0a when we write 'ch' because
// 'target' was opened in text mode
fprintf(target, "%c", ch);
}

Larry


It's been a few years since I worked with Windows, but on
further reflection I believe that you dont have to open
'source' in binary mode. this should be ok:

source = fopen(argv[1], "r");

That opens 'source' in text mode, any \r\n will be
converted to \n on reading, any \n not preceeded by \r
will be read unmodified.

Opening 'target' in text modes causes Windows to
automatically write \r\n everytime your prog writes a
newline (\n).

Larry
Jul 23 '05 #4

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

Similar topics

4
by: Christopher Finke | last post by:
I have a site wherein the clients can update the individual pages by editing the text of that page in a <textarea>. Then, when the page is displayed, all of the newlines (\n) are converted to HTML...
4
by: Grant Edwards | last post by:
I'm using xml.sax to parse the "datebook" xml file generated by QTopiaDesktop. When I look at the xml file, some of the attribute strings have newlines in them (as they are supposed to). ...
6
by: dbee | last post by:
So I can't seem to urlencode a file with newlines ... it just gives me a series of T_STRING unexpected parse errors... cat job_description | while read file ; do php -r "echo...
1
by: dbee | last post by:
Hi, So I'm having a problem with disappearing newlines. I import the newlines from a file into my shell script fine. But then I process the text and the url_encode comes out the other end with...
0
by: skip | last post by:
*argh!* I hate XML! There, now that that's off my chest... I am trying to save Python code as attributes of an XML tag with xml.dom.minidom machinery. The code, predicatbly enough, contains...
2
by: Edward K. Ream | last post by:
Hello all, I recently ran across a situation in which sax.saxutils.quoteattr did not work as I expected. I am writing Leo outlines as opml files http://en.wikipedia.org/wiki/OPML which forces...
1
by: HopfZ | last post by:
Not a question. I tested how two kinds of newlines (\n and \r\n) interact with three browsers: Fx (Firefox 2), Op (Opera 9), Ie (IE 7) (all three on Windows XP). Result: The string...
5
by: lister | last post by:
Hi, I am trying to output a newline, but this doesn't work: echo "Line1\nLine2"; Before anyone says, I know HTML doesn't recognise newlines. I don't need newlines in the rendered HTML, I...
4
by: -Lost | last post by:
For example: var newlines = 'a\n\nb\n\nc'; alert(newlines); Yet, if I get that *exact* same line from an XMLHttpRequest's responseText, it is always alerted as: a\n\nb\n\nc
4
by: lihao0129 | last post by:
Hi, folks: I recently went through a strange problem with my Javascript code, say: I have a string variable which are from a 'textarea' element and I want to remove the trailing newlines inside...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.