Thread: Why I couldn't see the correct format of a file?

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    8

    Unhappy Why I couldn't see the correct format of a file?

    Hi all,
    Now I'm coding a short program which can get the string from 2 column to 72 column in a file(total 80 column), but the run result is not right, the format of the outfile is not correct as a normal file.
    Can anyone kindly tell me where is the question in my program, thanks a lot.

    here is my program:

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

    //infile means input pli source file,outfile means analysys result
    FILE *infile;
    FILE *outfile;
    //FILE *result;

    char msg[80];
    char linebuf[73];

    char *ptr; //pointer ptr point to thr string linebuf

    int counter;

    void main(int argc,char * argv[])
    {
    if (argc < 2)
    {
    printf("No file specified\n\n");
    printf("Usage: cut inputfile-name outputfile-name\n");
    exit(1);
    }

    if ((infile = fopen(argv[1],"r")) == NULL)
    {
    printf("Can't open %s",argv[1]);
    exit(1);
    }

    if ((outfile = fopen(argv[2],"w")) == NULL)
    {
    printf("Can't open %s",argv[2]);
    exit(1);
    }

    ptr = NULL;

    while(!feof(infile))
    {
    ptr = linebuf;

    if (fgets(msg,81,infile))
    {
    for (counter = 0;counter < 72;counter++)
    {
    if (counter == 0)
    continue;
    else
    {
    *ptr = msg[counter];
    ptr++;
    }
    }
    *ptr = NULL;

    fputs(linebuf,outfile);

    }
    }
    fclose(infile);
    fclose(outfile);

    printf("\nGood job has done\n");

    }

  2. #2
    Unregistered
    Guest
    your output file, is it all in one line?

    e.g. instead of

    abc
    def
    ghi
    ...


    you get this?

    abcdefghi...

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    8

    Smile

    Yes, you're right.

    But I'd resovle this problem now.

    It just need add a sentence "*ptr = '\n';" after the every loop.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM