Correcting newlines

W

Wondiws

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);
}
 
A

ambar.shome

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
 
L

Larry I Smith

Wondiws said:
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
 
L

Larry I Smith

Larry said:
Wondiws said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top