Thread: string return in a function?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    UK
    Posts
    3

    string return in a function?

    hi im trying to retreive data from a file using a function call but it doesnt return the data back to the main(). ive tried changing some varriables to pointers and vice versa but the moment of return gives me what the input was, not the info i want to be returned,im a bit lost at the moment can anyone help? please?

    Code:
     char searcher(char *ItemDat);
     char retdat[250];
     char ItemId[250]; 
     char FTextdata[250];
     FILE*ReadDatFP;
    
    main ()   
      {     
      printf("Enter Item Id: ");
      scanf("%s",&ItemId);
      searcher(ItemId);
      printf("\n\nMain Output:\n%s\n",ItemId);
      system("pause");
      }
    
    
    char searcher(char *ItemDat)
    {
     if((ReadDatFP=fopen("TEST.txt","r"))==NULL)
       {
       printf("File could not be opened\n");
       system("pause");
       }
     else
       {
       printf("File could be opened\n");
       system("pause");       
       do
         {
         fscanf(ReadDatFP,"%s",FTextdata)!=EOF;
         }
       while(strcmp(ItemDat,FTextdata)!=0);
       }
     fscanf(ReadDatFP,"%s",retdat)!=EOF;
     printf("\nFunction output:\n%s\n",retdat);
     system("pause");  
     fclose(ReadDatFP);              
     return *retdat;
    }

    The system pauses were for direction diagnosis in my program and are not intended as part of the real program, Thanks for any help on this it is my first post.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> main ()

    Just FYI, 'main' returns an 'int'.

    >> scanf("%s",&ItemId);

    The name of an array already resolves to pointer, so you can drop the amperstand.

    >> if((ReadDatFP=fopen("TEST.txt","r"))==NULL)

    Since the later call to fclose would be incorrect in that case, you should probably just return from the function at that point.

    >> fscanf(ReadDatFP,"%s",FTextdata)!=EOF;

    The comparison there has no effect. Anyway, you'll want to break out of the loop if EOF is reached.

    >> fscanf(ReadDatFP,"%s",retdat)!=EOF;

    Again, no effect.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    UK
    Posts
    3
    thanks for the relpy.
    ive changed 'main()' to 'int main (void)' and put in 'return 0;'
    i removed the ampersand 'scanf("%s",ItemId);'
    i took out 'return *retdat;' and put in 'strncpy(ItemDat, retdat, 250);'
    and it now sends the string back to my main program.
    thanks for ure input Sebastiani.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    UK
    Posts
    3
    Thanks for ure input Elysia.
    I havent programmed in c for 4 years im alittle rusty XD.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM