Results 1 to 4 of 4

Thread: How Can I write strings in a new line with C Ansi ?e

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Location
    Brasil
    Posts
    3

    Question How Can I write strings in a new line with C Ansi ?e

    I wanna write string in a new line. How can i do that ?


    int main(int argc, char* argv[])
    {
    FILE *ponteiro_arquivo;

    int i =0;

    strcpy(sLinha, "0001 123456 nf 0,12\n"); /* copy 1 line*/

    for (i =0 ; i < 5; i++)
    if((ponteiro_arquivo = fopen ("c:\\gravar.txt", "w"))!= NULL){
    fprintf(ponteiro_arquivo, sLinha);
    /*always writing only the last line */
    fclose (ponteiro_arquivo);
    }


    Thanks for help me

  2. #2
    Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    63
    You are overwriting the file in each iteration of the loop. Try moving the open and close statements outside of your loop.

    Or you can open the file for "write/append", in which case your printf will simply add a line at the end of the file. In your example, both of these methods would produce the same output.

    Hope this helps,
    Cedric

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Location
    Brasil
    Posts
    3

    iT'S RUN

    THANKS FOR YOUR HELP... IT'S RUN NOW.

    I JUST OPEN THE FILE WITH OPTION "a".

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    it's still bad because it leaves with a lot of opened files hanging around - VERY bad! And it causes you to reopen it often - VERY slow!

    Code:
    int main(int argc, char* argv[]) 
    { 
    FILE *ponteiro_arquivo; 
    
    int i =0; 
    
    strcpy(sLinha, "0001 123456 nf 0,12\n"); /* copy 1 line*/ 
    if((ponteiro_arquivo = fopen ("c:\\gravar.txt", "wt"))== NULL)
    {
      printf("Error!\n");
      return -1;
    }
    for (i =0 ; i < 5; i++) 
    { 
    fprintf(ponteiro_arquivo, sLinha); 
    /*always writing only the last line */ 
    } 
    fclose (ponteiro_arquivo); 
    // ...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width