Thread: C file handling concept need help with specified writing format

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    1

    C file handling concept need help with specified writing format

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct str_in{
        char str[100];
        int num;
        };
    
    int main()
    {
        struct str_in str_in_ar[3];
        int i;
        char ap_str[200];
        FILE *fp;
    
        for(i=0;i<3;i++)
        {
            printf("Enter a string(100 character limit): ");
            fflush(stdin);
            gets(str_in_ar[i].str);
            printf("Enter a number: ");
            fflush(stdin);
            scanf("%d",&str_in_ar[i].num);
        }
    
        printf("Writing it to file.....");
    
        fp = fopen("str_1.txt","a");
        if(fp)
        {
            for(i=0;i<3;i++)
              strcpy(ap_str,str_in_ar[i].str);
              strcat(ap_str, " ");
              strcat(ap_str, (char)str_in_ar[i].num);
              //strcat(ap_str, "\n");
              fputs(ap_str,fp);
              memset(ap_str,0,200);
            fclose(fp);
        }
    
        return 0;
    }

    With this code what i'm trying to do is to write a line with a string followed by integer and then similarly to the few lines into a txt file.
    i hope this ->( .Screenshot by Lightshot ) makes it clear.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Don't use gets()
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    Don't use fflush(stdin)
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    > strcat(ap_str, (char)str_in_ar[i].num);
    Casting a number (like 1234) doesn't transform it to a string (like "1234").
    Honestly, you could do all your second loop with a single call to fprintf().

    > fclose(fp);
    Why is this inside your loop?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Salem View Post
    > fclose(fp);
    Why is this inside your loop?
    If you pay close attention, it actually isn't.

    Code:
            for(i=0;i<3;i++)
              strcpy(ap_str,str_in_ar[i].str);
              strcat(ap_str, " ");
              strcat(ap_str, (char)str_in_ar[i].num);
              //strcat(ap_str, "\n");
              fputs(ap_str,fp);
              memset(ap_str,0,200);
            fclose(fp);
    Devoted my life to programming...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Quote Originally Posted by GReaper View Post
    If you pay close attention, it actually isn't.

    So it is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help file concept
    By helloworld28 in forum C Programming
    Replies: 5
    Last Post: 04-01-2011, 10:25 AM
  2. file concept
    By munna_dude in forum C Programming
    Replies: 20
    Last Post: 06-16-2010, 09:01 AM
  3. Writing, passing, and handling matrices???
    By jlrobe21 in forum C Programming
    Replies: 4
    Last Post: 09-27-2005, 05:46 AM
  4. File I/O - Concept
    By MethodMan in forum C Programming
    Replies: 0
    Last Post: 03-02-2003, 12:07 PM
  5. writing a utility for format a file
    By powinda in forum C Programming
    Replies: 8
    Last Post: 02-12-2003, 06:55 PM

Tags for this Thread