Problem in FILE I/O

This is a discussion on Problem in FILE I/O within the C Programming forums, part of the General Programming Boards category; srry for double post but my first program was just base but now i made it multi dimensional to save ...

  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 08: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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    I think I need an extra set of brackets to evaluate ptr
    Only if you want to add != NULL
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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 08: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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    if(yesno)

    And what do you do to make this if fail?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,185
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    'n' is still true value because it is different from 0
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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 ?

Page 1 of 2 12 LastLast
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, 08:01 AM
  2. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 03: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, 01:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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