Thread: File Handling question

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    13

    File Handling question

    So i have a txt file with this in it:
    Code:
    1. Assamese/Asomiya
    2. Bengali/Bangla
    3. Bodo
    4. Dogri
    5. Gujarati
    6. Hindi
    7. Kannada
    8. Kashmiri
    9. Konkani
    10. Maithili
    11. Malayalam
    12. Manipuri
    13. Marathi
    14. Nepali
    15. Oriya
    16. Punjabi
    17. Sanskrit
    18. Santhali
    19. Sindhi
    20. Tamil
    21. Telugu
    22. Urdu
    and i would like to copy those which begins with 'H' or 'S' to an out.txt file with the numbers. I have the solution,but i would like to ask why the lang contains only the names without numbers?and the num contains only numbers... when i used the same scanf, with my logic it makes non sense,because it should copy the same information but obviously not. so the question is,why?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {   int endoffile=1;
        FILE *fp;
        FILE *out;
        char lang[30];
        char num[30];
        out=fopen("c:\\out.txt","w");
    fp=fopen("c:\\bhaarat.txt", "r+a");
    while(endoffile==1)
    {
        endoffile=fscanf(fp,"%s",&num);
        endoffile=fscanf(fp,"%s",&lang);
        if(lang[0]=='S' || lang[0]=='H')
            fprintf(out,"%s %s\n",num,lang);
    }
    fprintf(fp,"23. English");
    
        fclose(out);
       fclose(fp);
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Part of your problem is that you're not properly using the fscanf() call. Remember this function requires the address of the variable not a pointer to the address.

    main.c||In function ‘int main()’:|
    main.c|14|warning: format ‘%s’ expects argument of type ‘char*’, but argument 3 has type ‘char (*)[30]’ [-Wformat=]|
    main.c|15|warning: format ‘%s’ expects argument of type ‘char*’, but argument 3 has type ‘char (*)[30]’ [-Wformat=]|
    ||=== Build finished: 0 errors, 2 warnings (0 minutes, 0 seconds) ===|
    Remember when dealing with C-strings the name of the variable is the address, you don't need the ampersand.


    Jim

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you also have to check retrun value of scanf before using data read
    Code:
    while(fscanf(fp,"%29s %29s",num,lang) == 2)
    {
        if(lang[0]=='S' || lang[0]=='H')
            fprintf(out,"%s %s\n",num,lang);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02:01 PM
  2. File handling question...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2008, 02:02 AM
  3. file handling in C
    By cutelucks in forum C Programming
    Replies: 6
    Last Post: 04-13-2007, 11:35 AM
  4. FILE handling
    By Munisamy in forum C Programming
    Replies: 0
    Last Post: 02-28-2005, 12:04 AM