Thread: scanning values from a char array

  1. #1
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50

    Question scanning values from a char array

    Okay, part of my homework is to read from a logfile and output some specific information. (YES THIS IS HOMEWORK, AND NO, I DO NOT WANT YOU TO DO IT FOR ME! )

    I would just like to know if it's possible to scan through a char array, and assign values to variables from it.

    By the way, I haven't even tried to compile this yet, so I don't know if it even compiles. I'm pretty sure this won't work, as I'm passing fscanf() a char*, and not a FILE*.

    Code:
    void outputThings( char* line)
    { 
            int day, hour, min, sec;
            int ii;
    
            char* month;
            char* temp;
            char* process = (char*)malloc(30*sizeof( char));
            char* message = (char*)malloc(200*sizeof( char));
    
            fscanf( line,"%s %d %d:%d:%d    %s: %s", month, &day, &hour, &min, &sec, process, message);
    
            printf("%s %d %d:%d:%d %s: %s\n", month, day, hour, min, sec, process, message);
    
    }
    As I said, I don't want you guys to do this for me, just tell me what function I should be using to scan the array, or tell me I'm going about it the wrong way.

    thanks,
    The Doctor

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    fscanf reads from a file
    sscanf reads from a string
    scanf reads from stdin
    All do the same thing in terms of conversions and assignments. It's only the source of the characters which differ.

    fgets() and sscanf() is a particularly useful combination.
    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.

  3. #3
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Thank you! I'm sure that's exactly what I needed to know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Indexing an Array, Based on Char Values from another.
    By Venatio in forum C Programming
    Replies: 3
    Last Post: 07-20-2012, 03:54 PM
  2. Scanning txt file values into an array
    By skmightymouse in forum C Programming
    Replies: 16
    Last Post: 04-28-2012, 01:38 PM
  3. Sending Values and Scanning an Array
    By xxxixpats in forum C Programming
    Replies: 7
    Last Post: 11-05-2010, 11:32 PM
  4. fill unsigned char array with hex values
    By luigi40 in forum C Programming
    Replies: 1
    Last Post: 06-25-2005, 05:56 AM
  5. Comparing char values in an array using 'if'
    By Judy_528 in forum C++ Programming
    Replies: 18
    Last Post: 04-25-2002, 07:52 AM

Tags for this Thread