some problem with fwrite

This is a discussion on some problem with fwrite within the C Programming forums, part of the General Programming Boards category; after the user in put the name it is supposed to save it in xxx.txt file on the a drive ...

  1. #1
    Unregistered
    Guest

    Unhappy some problem with fwrite

    after the user in put the name it is supposed to save it in xxx.txt file on the a drive then ask user to put the a second name
    i think this program should work but it isn't
    could anyone help me please

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

    void Name(char nombre[16]);
    struct Manufacturer
    {
    char name[16];
    int position;
    bool pass;
    };
    void main()
    {
    struct Manufacturer m;
    m.pass=true;
    FILE *f;
    f=fopen("a:\\xxx.txt","a");
    Name(m.name);
    while (strlen(m.name)>0)
    {
    for(m.position=0;m.position<100;m.position++)
    {
    m.position;
    fwrite(&m, sizeof(m),1,f);
    Name(m.name);
    }
    }
    fclose(f);
    }
    void Name(char nombre[16])
    {
    bool r=true;
    do
    {
    r=true;
    puts("Enter Manufacturer name: ");
    gets(nombre);
    if (strlen(nombre)>=16)
    {
    printf("Manufacturer name must be less than 15 chars \n");
    r=false;
    }
    }while(!r);
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: some problem with fwrite

    i think this program should work but it isn't
    Please can you elaborate! What does it do wrong? People here like fixing bugs, but they have to know where to start, so give us a clue!

    void main()
    Don't void ya main. Declare it like this if you don't want to use the command line args:
    int main(void)
    And don't forget to return a value when you leave main.

    gets(nombre);
    gets does not do input size checking, so you've built in a case for a classic buffer overflow bug. (What happens to your buffer if the user enters 32 characters instead of 16?)

    could anyone help me please
    Sure, just sort point 1 first.

    Lastly, please use code tags when posting code here.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    Lightbulb

    u open the files with a but it should be ab

    f=fopen("a:\\xxx.txt","ab");

    insetd of

    f=fopen("a:\\xxx.txt","a");

    because fread and fwrite can only be used on binary files
    Parinoia Means Having All The Facts!

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