File convertor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ToKkE
    New Member
    • Sep 2006
    • 2

    #1

    File convertor

    Hello All,

    I'm not so in the programming. So I hope someone can help me here.
    I've to convert a file with data in it. You can see an example of data here:

    1.40363E+00 -5.36068E+00 -1.18413E+01 -1.09006E+01 -4.25570E+00 7.63039E+00
    1.85011E+01 1.66495E+01 5.28602E+00 1.83667E+00 -1.71721E+00 -1.23042E+01

    The problem with this data is that sometimes it's delimited with 1 or 2 spaces. So what I would like to have is to make a new file with the same data but the spaces replaced by 1 comma (also when there are 2 spaces in between).

    Thanks in advanced

    ToKkE
  • pukur123
    New Member
    • Sep 2006
    • 61

    #2
    Try this one.....


    #include<stdio. h>

    int main()
    {
    FILE *fp, *ft;
    int cnt=0;;
    char ch;

    fp=fopen("temp. txt","rt");
    ft=fopen("targe t.txt","wt");

    while(1)
    {
    while((ch=getc( fp)) != EOF)
    {
    if(ch != ' ' && ch != '\t' && ch != '\n')
    break;
    }

    if(ch == EOF)
    break;

    if(cnt >= 1)
    putc(',', ft);

    do
    {
    if(ch == ' ' || ch == '\t' || ch == '\n')
    break;
    putc(ch, ft);
    }
    while((ch=getc( fp)) != EOF);

    cnt++;

    if(ch == EOF)
    break;
    }

    putc(EOF, ft);

    fclose(fp);
    fclose(ft);

    return 0;
    }

    Comment

    • ToKkE
      New Member
      • Sep 2006
      • 2

      #3
      Originally posted by pukur123
      Try this one.....

      }
      Thanx,

      I'll try this...

      regards

      Comment

      • risby
        New Member
        • Sep 2006
        • 30

        #4
        Originally posted by pukur123
        Code:
        while(1) {
        	while((ch=getc(fp)) != EOF) {
        	}
        	if(ch == EOF)
        	   break;
                }
        [b]putc(EOF, ft);[/b]
        return 0;
        Pukur, when getc() returns EOF it is to indicate that it has read all the characters there are in the input file. It doesn't mean that it actually read a character with the value EOF from the input file and, therefore, it is not correct to write a character with the value EOF on the output file.

        Also, your code is not preserving newlines.

        Hope this helps

        Comment

        Working...