#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Help: extracting string from file


    HI
    I'm a newbie and don't have much knowledge in c and I'm stuck.
    I need to extract a string saved in a file , get its length and then add extra character(like zero) as a padding till it reach some size.
    I have been trying to do it like this but it is not complete, I don't know how to get it to work. can you please help me
    Code:
     FILE *file = fopen( argv[1], "r" );
    
            if ( file == 0 )
            {
                printf( "Could not open file\n" );
            }
            else 
            {
                int x;
              
                while  ( ( x = fgetc( file ) ) != EOF )
                {
                    printf( "%c", x );
                }
                fclose( file );
    Thanks in advance.
    Regards
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    If you want to save the string for later, you need to get an array for it

    Code:
    char data[100]; /* enough space for a string with length up to 99 */
    Also you need to put ecah character read from the file in there.

    Have fun!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0
    Thank you very much.

    regards

IMN logo majestic logo threadwatch logo seochat tools logo