Text File Conversion Problem 
Author Message
 Text File Conversion Problem

FIRST OF ALL - I CAN NOT PUT THIS MESSAGE ON NEWSGROUP...SORRY...

[I'm not even going to *try* to explain that.  -mod]

Text File  Conversion Problem

I want to ask anyone very simple question:
How to rewrite text file by replacing each characters by some law?
I try to write program using getc, putc and so far I can write "double file"
- old characters and new one - no real replacement:

#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>

const char FILE_NAME[80];

int main()
{
  int c = 0;
  int count = 0;      /* number of characters seen in file */
  FILE *in_file;      /* analyzed memory on address in_file */
  char ch;        /* character or EOF flag from text file */
  long offset, length;

  printf("\n\tType File Name (case sensitive) to be converted:");
  scanf("%s", FILE_NAME);     /* File Name to analyze */

  in_file = fopen(FILE_NAME, "rt+");
  if (in_file == NULL)
  {
     printf("Can not open %s\n", FILE_NAME);
     exit(0);
  }

  in_file = fopen(FILE_NAME, "rt+");
  printf("\nThis is a file in conversion:\n");
  while (1)
  {
     ch = (char)getc(in_file);
     printf("%c",ch);
     if (ch == EOF)
     {
        break;
     }
     else
     {
        switch(ch)         /* replace */
        {
           case '6':
              ch = '1';
              putc(ch,(in_file-1));
              break;
           case 'B':
              ch = '2';
               putc(ch,(in_file-1));
               break;
           case 'r':
               ch = '8';
               putc(ch,(in_file-1));
               break;
           case 'g':
                ch = '5';
                putc(ch,(in_file-1));
                break;
           default:
                ch = ch;
        }
        printf("%c",ch);
     }
  }
  fclose(in_file);

What's wrong with that?
John

--



Fri, 10 Jan 2003 03:00:00 GMT  
 Text File Conversion Problem


Quote:
>I want to ask anyone very simple question:
>How to rewrite text file by replacing each characters by some law?
>I try to write program using getc, putc and so far I can write "double file"
>- old characters and new one - no real replacement:

This sounds awfully like a homework assignment so I am going to restrict
myself to a couple of hints.

Create a struct to hold pairs of chars, the original and the
replacement.

Create an array of all possible originals sorted in some way (ASCII code
would do fine)

Use a binary search to find the original and write out the corresponding
replacement.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Sat, 11 Jan 2003 03:00:00 GMT  
 Text File Conversion Problem
I don't think this is right...
   putc(ch,(in_file-1));

....in_file is a pointer to a FILE stream, ie your subtracting one from a
pointer to a file structure, in_file will just stop pointing to your
FILE structure if you do that.

you want to look at fseek/ftell or fsetpos/fgetpos I think to reposition
the 'write' file pointer.

Also your openning the file twice, no need.

hope this helps
dom

Quote:

> FIRST OF ALL - I CAN NOT PUT THIS MESSAGE ON NEWSGROUP...SORRY...

> [I'm not even going to *try* to explain that.  -mod]

> Text File  Conversion Problem

> I want to ask anyone very simple question:
> How to rewrite text file by replacing each characters by some law?
> I try to write program using getc, putc and so far I can write "double file"
> - old characters and new one - no real replacement:

> #include <conio.h>
> #include <string.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <graph.h>

> const char FILE_NAME[80];

> int main()
> {
>   int c = 0;
>   int count = 0;      /* number of characters seen in file */
>   FILE *in_file;      /* analyzed memory on address in_file */
>   char ch;        /* character or EOF flag from text file */
>   long offset, length;

>   printf("\n\tType File Name (case sensitive) to be converted:");
>   scanf("%s", FILE_NAME);     /* File Name to analyze */

>   in_file = fopen(FILE_NAME, "rt+");
>   if (in_file == NULL)
>   {
>      printf("Can not open %s\n", FILE_NAME);
>      exit(0);
>   }

>   in_file = fopen(FILE_NAME, "rt+");
>   printf("\nThis is a file in conversion:\n");
>   while (1)
>   {
>      ch = (char)getc(in_file);
>      printf("%c",ch);
>      if (ch == EOF)
>      {
>         break;
>      }
>      else
>      {
>         switch(ch)         /* replace */
>         {
>            case '6':
>               ch = '1';
>               putc(ch,(in_file-1));
>               break;
>            case 'B':
>               ch = '2';
>                putc(ch,(in_file-1));
>                break;
>            case 'r':
>                ch = '8';
>                putc(ch,(in_file-1));
>                break;
>            case 'g':
>                 ch = '5';
>                 putc(ch,(in_file-1));
>                 break;
>            default:
>                 ch = ch;
>         }
>         printf("%c",ch);
>      }
>   }
>   fclose(in_file);

> What's wrong with that?
> John

> --


--



Sat, 11 Jan 2003 03:00:00 GMT  
 Text File Conversion Problem

Quote:

> FIRST OF ALL - I CAN NOT PUT THIS MESSAGE ON NEWSGROUP...SORRY...

> [I'm not even going to *try* to explain that.  -mod]

> How to rewrite text file by replacing each characters by some law?

It's going to be very tricky to do so efficiently without using a second
file. You'd have to move back and forth between read and write mode, and
do fseek() all the way.

Quote:
> I try to write program using getc, putc and so far I can write "double file"
> - old characters and new one - no real replacement:

> #include <conio.h>

Not standard.

Quote:
> #include <string.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <graph.h>

Not standard, either. You don't use either of these non-standard
headers; why don't you simply get rid of them?

Quote:
> const char FILE_NAME[80];

Eh? You've got an array to const characters, and you don't initialise
it. In effect, you've got a useless array, because you can't do a thing
with it.

Quote:
> int main()

int main(void) is better, though the above is legal.

Quote:
> {
>   int c = 0;
>   int count = 0;      /* number of characters seen in file */
>   FILE *in_file;      /* analyzed memory on address in_file */
>   char ch;        /* character or EOF flag from text file */

Nope. char ch holds a character. It may or may not be able to hold EOF.
Make this an int.

Quote:
>   long offset, length;

>   printf("\n\tType File Name (case sensitive) to be converted:");

For maximum portability, insert fflush(stdout); here.

Quote:
>   scanf("%s", FILE_NAME);     /* File Name to analyze */

You can't do this; FILE_NAME is an array of const chars, which you can't
legally change. Remove the const from the declaration of FILE_NAME and
you're almost ok.
Almost, because you also allow your user to enter a random length string
into an 80 byte memory area, quite possibly resulting in a buffer
overflow. Use "%.80s" instead.

Quote:
>   in_file = fopen(FILE_NAME, "rt+");

The t modifier is not portable and in fact unnecessary; files are text
unless you ask for them to be binary.

Quote:
>   if (in_file == NULL)
>   {
>      printf("Can not open %s\n", FILE_NAME);
>      exit(0);
>   }

>   in_file = fopen(FILE_NAME, "rt+");

What, again? You've already done this. Either this open will fail, or
you've just lost a file handle.

Quote:
>   printf("\nThis is a file in conversion:\n");
>   while (1)
>   {
>      ch = (char)getc(in_file);
>      printf("%c",ch);
>      if (ch == EOF)

Again, nope. You have read from in_file either an unsigned char, cast to
int, or EOF. You then cast this value to char. Now you've got a problem:
if char is unsigned by default, ch is never going to contain EOF, and
your loop never ends. If, OTOH, char is signed, there is another value,
also a legal char, which is cast to the same value as EOF, and which
therefore will also end the loop, incorrectly.

The right way to go about this is to declare ch an int, and not to cast
the return from getc(). You then have either an unsigned char value in
ch, or EOF; never both. Now you can reliably compare ch to EOF and leave
the loop.
After that you can, if you wish, cast or assign ch to a char, if you
think it makes life easier, though it's required not to make a
difference for things like letters and digits, because they're in the
required character set and must therefore always be positive.

Quote:
>      {
>         break;
>      }
>      else
>      {
>         switch(ch)         /* replace */
>         {
>            case '6':
>               ch = '1';
>               putc(ch,(in_file-1));

What is this supposed to be? in_file-1 is a bit of weird pointer
arithmetic which cannot possibly work. What did you suspect would be the
result of in_file-1?

If you ask me, you'd be much better off just going for the two-files
option. Read from one file, write to another. If you do it right, you
can even make it a filter, and increase its usefulness even more.

Richard
--



Sat, 11 Jan 2003 03:00:00 GMT  
 Text File Conversion Problem
Oh boy...here we go...

Quote:

> Text File  Conversion Problem

> I want to ask anyone very simple question:
> How to rewrite text file by replacing each characters by some law?
> I try to write program using getc, putc and so far I can write "double file"
> - old characters and new one - no real replacement:

> #include <conio.h>
non-standard header
> #include <string.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <graph.h>
ibid

> const char FILE_NAME[80];

> int main()
> {
>   int c = 0;
>   int count = 0;      /* number of characters seen in file */
>   FILE *in_file;      /* analyzed memory on address in_file */
>   char ch;        /* character or EOF flag from text file */
>   long offset, length;

>   printf("\n\tType File Name (case sensitive) to be converted:");
>   scanf("%s", FILE_NAME);     /* File Name to analyze */

>   in_file = fopen(FILE_NAME, "rt+");
>   if (in_file == NULL)
>   {
>      printf("Can not open %s\n", FILE_NAME);
>      exit(0);
>   }

>   in_file = fopen(FILE_NAME, "rt+");
>   printf("\nThis is a file in conversion:\n");
>   while (1)
>   {
>      ch = (char)getc(in_file);

Do not cast the result before checking for EOF.
Quote:
>      printf("%c",ch);
>      if (ch == EOF)
>      {
>         break;
>      }
>      else
>      {
>         switch(ch)         /* replace */
>         {
>            case '6':
>               ch = '1';
>               putc(ch,(in_file-1));

WTF is "in_file-1"? It's certainly not a FILE* (except, perhaps, by dumb
luck). In fact, I would suspect that all your putc() calls are just
silently failing! (check the return values and see).
Quote:
>               break;
>            case 'B':
>               ch = '2';
>                putc(ch,(in_file-1));
>                break;
>            case 'r':
>                ch = '8';
>                putc(ch,(in_file-1));
>                break;
>            case 'g':
>                 ch = '5';
>                 putc(ch,(in_file-1));
>                 break;
>            default:
>                 ch = ch;

Huh? What is it that you expect _this_ to accomplish?
Quote:
>         }
>         printf("%c",ch);
>      }
>   }
>   fclose(in_file);

> What's wrong with that?
> John

> --


A couple of things: befre trying to fix the errors you have, might I
make a suggestion?
START OVER!
For one thing, massaging a file in-place tends not to be the best
approach; it would be better to rewrite the file (perhaps removing the
original once you're done).
Another quibble is that your control structure is a lot stranger than it
needs to be. In pseudo-code, consider the following:

open input_file
if not successful, die.
open output_file
if not successful die

while the input isn't EOF
   change the character if needed
   write the character to output_file

close input_file
close output_file

HTH,
--ag
--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account for more info)

--
"I'd sooner fly another combat mission than ride the Cyclone again" --
Joseph Heller
--



Sat, 11 Jan 2003 03:00:00 GMT  
 Text File Conversion Problem
Thanks everybody.
And this is a working one...
Roman Tunkel

/*********************************************************************
*                                 prconv.c                                  *
*                                                                           *
**                      Compiled MSVC++ 1.52 - for Windows                   *
*                               **  IMPORTANT  **                           *
*                    It is running under Win 95 or Win NT.                  *
*                                                                           *
********************************************************************/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>

/*** MAIN ***/

int main(void)
{
   char FILE_NAME_IN[30];             /* random length string */
   char FILE_NAME_OUT[30];            /* random length string */
   int c = 0;
   int count = 0;         /* number of characters seen in file */
   FILE *in_file;         /* memory on address in_file */
   FILE *out_file;        /* memory on address out_file */
   int ch;                /* characters or EOF flag from text file */
   long offset_in, offset_out;

   printf("\n\tType File Name (case sencitive) to be read:");
   scanf("%s", FILE_NAME_IN);     /* File Name to analyze */
   printf("\n\tType File Name (case sencitive) to be created:");
   scanf("%s", FILE_NAME_OUT);     /* File Name to analyze */

   in_file = fopen(FILE_NAME_IN, "r");
   out_file = fopen(FILE_NAME_OUT, "w");

   if (in_file == NULL)
   {
      printf("Can not open %s\n", FILE_NAME_IN);
      exit(0);
   }

   if (out_file == NULL)
   {
      printf("Can not create %s !!!\n", FILE_NAME_OUT);
      exit(0);
   }

   offset_in = ftell(in_file);
   offset_out = ftell(out_file);
   fseek(in_file, offset_in, SEEK_SET);
   fseek(out_file, offset_out, SEEK_SET);

   printf("This is a file before conversion:\n");
   while (1)
   {
      ch = fgetc(in_file);
      printf("%c",ch);
      if (ch == EOF)
      {
         break;
      }
      count++;
   }
   printf("\nNumber of characters in %s (spaces included) is %d\n",FILE_NAME_IN,
count);
   fclose(in_file);

/* creating converted file */

   in_file = fopen(FILE_NAME_IN, "r");
   printf("\nThis is a file in conversion:\n");
   while (1)
   {
      ch = fgetc(in_file);
      if (ch == EOF)
      {
         break;
      }
      else
      {
         switch(ch)
         {
            case 'a':
               ch = 'a';
        break;
            case 'A':
               ch = 'A';
        break;
            case '6':
               ch = '?';
        break;
            case 'B':
        ch = '?';
        break;
            case 'r':
        ch = '?';
        break;
            case 'g':
        ch = '?';
        break;
            case 'D':
        ch = '?';
        break;
            case '3':
               ch = '?';
        break;
            case 'k':
               ch = '?';
               break;
            case 'K':
               ch = '?';
               break;
            case 'M':
               ch = '?';
               break;
            case 'm':
               ch = '?';
               break;
            case 'H':
               ch = '?';
               break;
            case 'o':
               ch = 'o';
        break;
            case 'O':
               ch = 'O';
        break;
            case 'n':
               ch = '?';
               break;
            case 'c':
               ch = 'c';
        break;
            case 'C':
               ch = 'C';
        break;
            case 'T':
               ch = '?';
               break;
            case 'y':
               ch = '?';
               break;
            case 'Y':
               ch = '?';
               break;
            case 'f':
               ch = '?';
               break;
            case 'F':
               ch = '?';
        break;
            case '4':
               ch = '?';
               break;
            case 'R':
               ch = '?';
               break;
            default:
               ch = ch;
         }

         fputc(ch,(out_file));
         printf("%c",ch);

      }
   }
   fclose(in_file);
   fclose(out_file);

/* checking output */

   out_file = fopen(FILE_NAME_OUT, "rt");
   printf("\n\nThis is a file after conversion:\n");
   while (1)
   {
      ch = fgetc(out_file);
      printf("%c",ch);
      if (ch == EOF)
      {
         break;
      }
   }
   fclose(out_file);
   return(0);

Quote:
}

/*************************************************/

Quote:



> >I want to ask anyone very simple question:
> >How to rewrite text file by replacing each characters by some law?
> >I try to write program using getc, putc and so far I can write "double file"
> >- old characters and new one - no real replacement:

> This sounds awfully like a homework assignment so I am going to restrict
> myself to a couple of hints.

> Create a struct to hold pairs of chars, the original and the
> replacement.

> Create an array of all possible originals sorted in some way (ASCII code
> would do fine)

> Use a binary search to find the original and write out the corresponding
> replacement.

> Francis Glassborow      Association of C & C++ Users
> 64 Southfield Rd
> Oxford OX4 1PA          +44(0)1865 246490
> All opinions are mine and do not represent those of any organisation
> --


--



Tue, 14 Jan 2003 03:00:00 GMT  
 Text File Conversion Problem


[ ... ]

Quote:
>          switch(ch)
>          {
>             case 'a':
>                ch = 'a';
>         break;
>             case 'A':
>                ch = 'A';
>         break;
>             case '6':
>                ch = '?';
>         break;
>             case 'B':
>         ch = '?';
>         break;

As far as I can see, reading through your switch statement, each
character is overwritten with either itself (i.e. even though you've
got a case for it, it's not changed) or else has a question mark
substituted for it.  With this given, I'd probably cheat and do
something like:

        char subs[] = "BrgD3KkMmHnTYy4R";

// ...

while (EOF != (ch=fgetc(infile))) {
        if ( strchr(subs, ch) != NULL)
                ch = '?';
        fputc(ch, outfile);

Quote:
}

If you're more interested in speed and don't mind using a little
extra data space to achieve it, you can write out a complete
translation table, so your loop would look something like:

        while (EOF!=(ch=fgetc(infile)))
                fputc(trans_table[ch], outfile);

but given the relatively small number of substitutions you're looking
at, this probably isn't really worthwhile unless you're processing a
LOT of data.

--
    Later,
    Jerry.

The universe is a figment of its own imagination.
--



Wed, 15 Jan 2003 03:00:00 GMT  
 Text File Conversion Problem


Quote:
>Thanks everybody.
>And this is a working one...
>Roman Tunkel

Could you explain why you have the characters in an apparently random
order? In addition why all the cases of replacing a character by itself
when this is the default?
Come to that why the self assignment in the default?

Maybe it works but it would not pass any code review from me and if it
was a homework question it would get about 30% (and I think I would
score quite a few non-working programs at 60% or more). Working is not
enough. The code must be comprehensible and it must be maintainable.

If you think I am being unkind, I would argue that anyone who accepted
your code without comment was doing you a much bigger disservice. To
your credit you wrote the program, you sought reasonable help and you
were not afraid to show your results. Now write the second version that
should be much cleaner.

For a starter:

Initialise to arrays of char with the original symbols and the
replacements. Now locate the input character in the first array (a
linear search will do for starters, we can make it more sophisticated if
measurement of performance shows that it is necessary but I append a
much faster method for ASCII or EBCDIC character codings (you need to
know which)

When you have determined the position of the input character you can
just index the second array to get the output (replacement one).

Faster, but coding specific:

Use a single 256 byte array which contains the replacement character for
each of the possible 256 character codes (some will not be printable but
covering them all keeps things simple. Now index the array with the code
for the input character.

Code fragment:

char output[256] = "????????????????????????????????xV12 etc.
char ch;
ch = fgetc(infile);
putc(output[ch], outfile);

How much simpler do you want it?

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Wed, 15 Jan 2003 03:00:00 GMT  
 Text File Conversion Problem
On 29 Jul 2000 04:22:47 GMT, Francis Glassborow

Quote:

> Use a single 256 byte array which contains the replacement character for
> each of the possible 256 character codes (some will not be printable but
> covering them all keeps things simple. Now index the array with the code
> for the input character.

> Code fragment:

> char output[256] = "????????????????????????????????xV12 etc.

This array could also be initialized from your original set of pairs
possibly obtained from a configuration file at run time.

Quote:
> char ch;
> ch = fgetc(infile);
> putc(output[ch], outfile);

Minor problems with things like \n would of course be covered in the
array initialization.

Quote:
> How much simpler do you want it?

It may be a bit harsh to expect a beginner to see an array as a
discrete function mapping the domain 0... to some range.  This is
even more true in modern languages which hide this by using
different tokens for calling the discrete functions.  If the
original code had used a function then

   putc(output(ch), outfile);

would have required token changes in C but not some older languages.

John
--



Wed, 15 Jan 2003 03:00:00 GMT  
 Text File Conversion Problem


Quote:


> >Thanks everybody.
> >And this is a working one...
> >Roman Tunkel

> Could you explain why you have the characters in an apparently
> random order? In addition why all the cases of replacing a
> character by itself when this is the default?
> Come to that why the self assignment in the default?

> Maybe it works but it would not pass any code review from me and
> if it was a homework question it would get about 30% (and I think
> I would score quite a few non-working programs at 60% or more).
> Working is not enough. The code must be comprehensible and it must
> be maintainable.

> If you think I am being unkind, I would argue that anyone who
> accepted your code without comment was doing you a much bigger
> disservice. To your credit you wrote the program, you sought
> reasonable help and you were not afraid to show your results.
> Now write the second version that should be much cleaner.

> For a starter:

> Initialise to arrays of char with the original symbols and the
> replacements. Now locate the input character in the first array
> (a linear search will do for starters, we can make it more
> sophisticated if measurement of performance shows that it is
> necessary but I append a much faster method for ASCII or EBCDIC
> character codings (you need to know which)

> When you have determined the position of the input character you
> can just index the second array to get the output (replacement one).

> Faster, but coding specific:

> Use a single 256 byte array which contains the replacement character
> for each of the possible 256 character codes (some will not be
> printable but covering them all keeps things simple. Now index the
> array with the code for the input character.

> Code fragment:

> char output[256] = "????????????????????????????????xV12 etc.
> char ch;
> ch = fgetc(infile);
> putc(output[ch], outfile);

> How much simpler do you want it?

Here comes a picking of nits :-)

The first decision is "what is likely to change in the future".  I
submit that the choice is a) the translations, or b) the system on
which the code is implemented.

If the choice is a), I have no major quibbles.  If b), we have to
consider character sets, wide characters, multi-byte characters, etc.
ad nauseam.  Here the switch implementation (with suitable use of
defaults) will be much more portable.

A third consideration is "who will read this code in the future". An
elegant and efficient algorithm may not be nearly as useful as ultra
clear and simple coding.

--

 http://www.qwikpages.com/backstreets/cbfalconer/
--



Sat, 25 Jan 2003 03:00:00 GMT  
 Text File Conversion Problem

writes

Quote:
>Here comes a picking of nits :-)

>The first decision is "what is likely to change in the future".  I
>submit that the choice is a) the translations, or b) the system on
>which the code is implemented.

>If the choice is a), I have no major quibbles.  If b), we have to
>consider character sets, wide characters, multi-byte characters, etc.
>ad nauseam.  Here the switch implementation (with suitable use of
>defaults) will be much more portable.

>A third consideration is "who will read this code in the future". An
>elegant and efficient algorithm may not be nearly as useful as ultra
>clear and simple coding.

I agree with your 'nits' and any good teacher should bring them out in
the discussion that should occur subsequent to homework being returned.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Sun, 26 Jan 2003 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. text file conversion to array of numbers

2. text file conversion

3. Text to Rich Edit conversion on read from file

4. A text conversion problem.

5. newbie:combobox text conversion to int problem

6. Problem with inserting text into a simple text file

7. Conversion from RTF-text to ANSI-text

8. Passing strings from a text file problem

9. Problem with saving data into a text file in pre-formatted form

10. Comparing two text files - problem

11. problem in printing text file

12. Fortran include file to C header file conversion

 

 
Powered by phpBB® Forum Software