fwrite problem

This is a discussion on fwrite problem within the C Programming forums, part of the General Programming Boards category; i am trying to write 2 strings to a file ,however after each string has been written i want to ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    67

    fwrite problem

    i am trying to write 2 strings to a file ,however after each string has been written i want to start a new line , i know that "\n" is the newline character however whenever i try and fwrite this it wont work. here is my code:

    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    FILE write(FILE *f, char *operand)
    {   
        fwrite(operand, 1, (strlen(operand)+1), bo);
        
    }
    
    void test()
    {
        FILE *f;
        if ( ( f = fopen("test.txt", "r+b" ) ) == NULL) {
    	     printf( "File Cannot Be Opened\n" );
        }
        int a = 1;
        int b = 2;
        if( a == 1){
          char a[20];
          strncpy( a, "00000", 20 ) ;
          write(f,a);
        }
        if( b == 2){
          char b[20];
          strncpy( b, "11111", 20 ) ;
          write(f,b);
        }    
    }
    int main(void)
    {   
        test();
    }
    the file test.txt currently shows 0000 11111 , however i want it to show:

    00000
    11111

    how can i put newlines in? thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how can i put newlines in?
    '\n', perhaps?
    My best code is written with the delete key.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    From what it looks like, it might be asier to write in ascii format, not binary. Unless you have your reasons for using binary.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    do i need to write the '\n' as a character or as a string? i'm assuming as a character, however doesnt fwrite only write strings?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    u cant do that puting newline in bimary format, i have opened the file in ASCII format
    Code:
      if ( ( f = fopen("test.txt", "aw" ) ) == NULL)
    and this is a code to intsrt a new line
    Code:
    FILE write(FILE *f, char *operand)
    {   
        fwrite(operand, 1, (strlen(operand)+1), f);
        fputc('\n',f);
          
    }
    s.s.harish

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    do i need to write the '\n' as a character or as a string? i'm assuming as a character, however doesnt fwrite only write strings?
    Code:
    {
      char c = '\n';
      fwrite(&c, 1, 1, f);
    }
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21