Thread: Weird error

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

    Weird error

    today i made FILE I/O but i wanted the user to press his like if he want name lol.txt it will make it as lol.txt i made it work but it only accept 10 char array when i change the fgets to lets say 100 it doesnt want to :S i dunno whats with this error
    Code:
    #include "inc.h"
    int main(void)
    {
        char name[100];
        FILE*fptr;
        printf("Where do u wanna save your file  ??");
        fputs("PLease enter your file name: ",stdout);
        fgets(name,10,stdin);
        fptr=fopen(name,"w");
        if(fptr==NULL)
    {
                      fprintf(stderr,"Coudlnt Access %s - because %s",name,sys_errlist[errno]);
                      exit(1);
    }
    fprintf(fptr," ");
    getchar();
    fclose(fptr);
        return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Let me guess - your filename is 9 characters long, and since your code doesn't remove the newline, it leaves the newline in the input buffer when you use 10, but when you put 100, you get a newline on the end of the filename, which makes the filename invalid, and thus the fopen() fails. That is of course just a guess.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    coz invalid arguments ??why is that and why have i got error opening ??

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    thanks i fixed it now
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
        char name[100];
        FILE*fptr;
        int i;
        printf("Where do u wanna save your file  ??");
        fputs("PLease enter your file name: ",stdout);
        fgets(name,100,stdin);
        for(i=0;i<100;i++)
        {
                          if(name[i]=='\n')
                          {
                                           name[i]=NULL;
                                           }
    }
        fptr=fopen(name,"w");
        if(fptr==NULL)
    {
                      fprintf(stderr,"Coudlnt Access %s - because %s",name,sys_errlist[errno]);
                      getchar();
                      exit(1);
    }
    fprintf(fptr," ");
    getchar();
    fclose(fptr);
        return 0;
    }
    it compiles fine with msv6 but with dev i get this error
    assignment makes integer from pointer without a cast

    i dunno why

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by lolguy View Post
    it compiles fine with msv6 but with dev i get this error
    assignment makes integer from pointer without a cast

    i dunno why
    Because NULL is a pointer type, and you are assigning it to a character. Instead of NULL use '\0' or just 0.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    Quote Originally Posted by brewbuck View Post
    Because NULL is a pointer type, and you are assigning it to a character. Instead of NULL use '\0' or just 0.
    thanks

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And writing code that loops through the entire string when there are nice functons like "strchr" that finds a character in the string as a standard function seems like asking for trouble.

    Also, you should probably use MAX_PATH instead of 100 - as that is the value of the max length of a (non-redundant) path+filename. Of course, if you throw in a load of . and .. in the filename, you can make it infinitely long, since you can append as many "/foo/../foo/.." as you like and still be in the same place.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    Quote Originally Posted by matsp View Post
    And writing code that loops through the entire string when there are nice functons like "strchr" that finds a character in the string as a standard function seems like asking for trouble.

    Also, you should probably use MAX_PATH instead of 100 - as that is the value of the max length of a (non-redundant) path+filename. Of course, if you throw in a load of . and .. in the filename, you can make it infinitely long, since you can append as many "/foo/../foo/.." as you like and still be in the same place.

    --
    Mats
    yah i was gonna change defines i ill do in end coz this is base of my program but thanks for the tip for strchr it sounds like cool function i ill look into it now

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i m trying to do the other part of the code but i get compiler error i think coz i m lil bad with multi dimensional arrays
    Code:
    #include <stdio.h>
    #include <stdlib.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;
    	for(i=0;i<10;i++)
    	{
                         printf("enter your Name of the file num %d",i+1);
                         scanf("%s",&name[i]);
                         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;
                                  }
    	}
        for(x=0;x<100;x++)
        {
                          if(name[x]=='\n')
                          {
                                           name[x]=0;
                                           }
    }
    fprintf(fptr," ");
    getchar();
    fclose(fptr);
        return 0;
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your crappy indentation is still vinegar, and not honey.
    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.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    scanf("%s",&name[i])
    name[i] is already pointer to char - no need to add &

    %s reads till the first white space - name could contain spaces
    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

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Apart from the sucky indentation...
    Scanf woes
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM