Thread: Problem in FILE I/O

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Problem in FILE I/O

    srry for double post but my first program was just base but now i made it multi dimensional to save many txts but i got problem in compiling 1 error
    anyways here the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(void)
    {
        char name[10][100];//we got 10 strings and each of them are length 100
        FILE*fptr;
        int i;
        char yesno;
    	int x;
    	char *pch;
    	for(i=0;i<10;i++)
    	{
                         printf("enter your Name of the file num %d",i+1);
                         scanf("%s",name[i]);
    					 if(strchr(name[i],'\n'))//thanks to mtsp for his tip
    {
    	name[i]=0;
    }
                         fptr=fopen(name[i],"w");
        if(fptr==NULL)
    {
                      fprintf(stderr,"Coudlnt Access %s - because %s",name,sys_errlist[errno]);
                      getchar();
                      exit(1);
    }
                         printf("Do you wanna continue ??");
                         scanf("%c",&yesno);
                         if(yesno=='N' || yesno=='n')
                         {
                                       break;
                                  }
    	}
    
    fprintf(fptr," ");
    getchar();
    fclose(fptr);
        return 0;
    }
    its probably in mutli dimensional since i m very bad at them

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lolguy View Post
    //thanks to mtsp for his tip
    Your implementation, unfortunately, is a lil' off. Since there is certainly a newline in name[i], the if statement will always evaluate as true because the pointer returner by strchr is not NULL. Then what happens?
    Code:
    name[i]=0;
    I am sure this is not what you want! You want to change the newline to a null terminator, so try something like this:
    Code:
    char *ptr=NULL;
    if (ptr=strchr(name[i],'\n')) ptr[0]=0;
    Last edited by MK27; 01-21-2009 at 09:22 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    scanf("%s",name[i]);
    if(strchr(name[i],'\n'))
    %s does not read white spaces - so no chance to have a new line character inside the string

    Code:
    scanf("%c",&yesno);
    %c does read white spaces - so in your case - it reads the new-line char left by the %s format of the previous scanf.

    Code:
    char *ptr=NULL;
    if (ptr=strchr(name[i])) ptr[0]=0;
    you forgot the second parameter '\n' of the strchr
    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

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vart View Post
    Code:
    char *ptr=NULL;
    if (ptr=strchr(name[i])) ptr[0]=0;
    you forgot the second parameter '\n' of the strchr
    Yep, and I think I need an extra set of brackets to evaluate ptr. I'll fix this in case the OP comes back...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I think I need an extra set of brackets to evaluate ptr
    Only if you want to add != NULL
    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

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vart View Post
    Only if you want to add != NULL
    I thought I had tested this before! Are you saying that even without those extra brackets,
    Code:
    if (ptr=strchr(name[i],'\n')) ptr[0]=0;
    will still only be true if ptr!=NULL

    I thought my compiler told me...nope, it didn't(if (ptr=NULL) is false). Thanks vart...I have to go erase some extra brackets around here somewhere...lots of them...
    Last edited by MK27; 01-21-2009 at 09:23 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    if(ptr)
    and
    Code:
    if(ptr != NULL)
    are essentially the same

    I actually prefer
    Code:
    ptr=strchr(name[i],'\n');
    if(ptr)
    more than

    Code:
    if (ptr=strchr(name[i],'\n'))
    becaus some compilers could give a warning that you have assignment where should be comparison
    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

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vart View Post
    becaus some compilers could give a warning that you have assignment where should be comparison
    Maybe that's where I picked up:
    Code:
    if ((ptr=strchr(name[i],'\n')))
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    srry back anyways thanks
    i fixed the code
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main(void)
    {
        FILE *fptr;
        char name[100][10];
        char *pch;
        int i;
        char yesno='n';
        for(i=0;i<10;i++)
        {
        printf("Please enter your [%d] name: ",i+1);
        scanf("%s",name[i]);
        pch=strchr(name[i],'\n');
        if(pch)
        {
               *pch=0;
               }
                                fptr=fopen(name[i],"w");
                                if(fptr==NULL)
                                {
                                              fprintf(stderr,"coudlnt acccess %s because %s",name,sys_errlist[errno]);
                                              exit(1);
                                              }
                                fprintf(fptr,"Hello world");
                  fputs("Do you wanna Save more files??",stdout);
                  getchar();
                  scanf("%c",&yesno);
                  if(yesno)
                  {
                            exit(1);
                            }
                                }
                                return getchar();
    }

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if(yesno)

    And what do you do to make this if fail?
    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

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    Quote Originally Posted by vart View Post
    if(yesno)

    And what do you do to make this if fail?
    it will just exit the program then

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lolguy View Post
    it will just exit the program then
    That's not the question. The question is, under what circumstances will yesno be false?

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i declared it up to be = to 'n' character when user press n it will exit if not will keep going till array finishes

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    'n' is still true value because it is different from 0
    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

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oh i havent checked that thanks so i have to chk :S ? in my function ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 09:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 04:32 PM
  3. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM