Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Substring extraction in C

  • 11-06-2007 11:40am
    #1
    Registered Users, Registered Users 2 Posts: 34


    Hi everyone,

    What I'm trying to do is as follows: I have a text file which contains dates followed by various values. I intended to read in the file line by line and extract the date and times so that they can be converted to Julian Dates.
    However I'm having some problem with extracting the necessary values out of the strings so that I might use them.

    The code that I have attached reads in the first line of the the text file
    (which is something like this: 21-Dec-01,16:00:05,000, 57, 4.8, 58, 0.0, 59, 0.0, 60, 0.0, 9, 0.19)
    I want to read the 21,01,16,00 & 05 into int variables and have something for the Dec part that results in 12 being passed to another int variable.

    I've tried referring directly to the values of the line array but that leads to a bus error. If anyone has any ideas or can point me in the right direction that'd be great.
    #include <stdio.h>

    int main(void)
    {

    char filename[25];
    FILE *fp;
    char string[128];
    int year, month, day;
    double hour, minute, second;
    int temp1, temp2, temp3;

    printf("Please enter name of data file to be converted to correct standard:\n>");
    scanf("%s", &filename);

    printf("\n %s\n", filename);
    fp = fopen(filename, "r");

    if ( fp == NULL )
    printf("File doesn't exist!\n");
    else
    {
    char line[256];

    fgets ( line, sizeof line, fp);

    printf("%s", line);

    fclose( fp );
    }

    return(0);
    }


Comments

  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    Egon wrote:
    Hi everyone,

    What I'm trying to do is as follows: I have a text file which contains dates followed by various values. I intended to read in the file line by line and extract the date and times so that they can be converted to Julian Dates.
    However I'm having some problem with extracting the necessary values out of the strings so that I might use them.

    The code that I have attached reads in the first line of the the text file
    (which is something like this: 21-Dec-01,16:00:05,000, 57, 4.8, 58, 0.0, 59, 0.0, 60, 0.0, 9, 0.19)
    I want to read the 21,01,16,00 & 05 into int variables and have something for the Dec part that results in 12 being passed to another int variable.

    I've tried referring directly to the values of the line array but that leads to a bus error. If anyone has any ideas or can point me in the right direction that'd be great.

    Well you need to tokenize the String the commas seem to be a good delimiter, have a look into the Stringtok() function


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    You don't need to tokenize the string is it's always expected in the same format.

    The function sscanf can be used. There's some info about it here that does an alright job of explaining it.


  • Registered Users, Registered Users 2 Posts: 1,287 ✭✭✭joe_chicken


    Agreed, you don't need to tokenize the string if it comes in the same format every time, plus the separators are different for each piece of info (i.e. colons and hyphens)...

    I'd just parse the character array, taking parts of it, putting it into a string and performing an "atoi" .


  • Registered Users, Registered Users 2 Posts: 34 Egon


    Got it working via sscanf, now to figure out how to convert Gregorian time and dates to Julian Dates...

    Cheers!


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Egon wrote:
    figure out how to convert Gregorian time and dates to Julian Dates...

    Quick, to the Googletron!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 34 Egon


    ok, I've got the program running, now I've come to another wee stumbling block. the program is needed to be run so that it automatically operates on data piped directly through to it.
    so, I thought, a unix shell script'd be prefect
    However I'm not that comfortable with shell scripting, and it would take me ages to write a script that would do the same as my C program. I know it's possible to call the program from within the shell script, but I've no idea how to pass the variable (to the which the input data has been put in) to the called program. I've checked the net and can't find anything, if anyone has any ideas it'd be great!

    My C code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>

    double julian(double day, double month, double year, double hour, double minute, double second);

    int main( void )
    {

    char *months[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; //String to change the month to a numerical value
    char filename[256]; //String to hold filename
    FILE *fp; //Pointer to open the above file for reading
    FILE *fpWrite; //Pointer for file to be written to
    char string[256]; //String to hold each line of input file
    double year, month, day;
    double hour, minute, second; //Defining variables to be used throughout the program
    int condition = 1;
    int count = 1;
    int i = 1;
    double startAverage = 0;
    double endAverage = 0;
    double average = 0;
    double presentAverage = 0;
    double julianDate = 0;
    double previousJulian = 0;
    double startJulian = 0; double endJulian = 0;
    double standard = 0;


    printf( "Please enter name of data file to be converted to NASA standard or 'quit':\n>" );
    scanf( "%s", &filename );

    fp = fopen( filename, "r" ); //Opens input file for reading
    fpWrite = fopen("NASA_Standard.txt", "w" ); //Opens file for writing

    if ( fp == NULL )
    printf( "File doesn't exist!\n " );
    else
    {
    char line[256]; //String to read in file line by line

    while(fgets ( line, sizeof line, fp) != NULL) //Read in file until no more lines left
    {
    sscanf ( line,"%lf -%3s -%lf,%lf:%lf:%lf,%*d, %*d, %lf",&day,&string,&year,&hour,&minute,&second,&presentAverage ); //Reads values from line into variables

    if( presentAverage < 0 ) //Ignores negative numbers for PM values
    presentAverage = 0;

    for(i = 0; i <= 12; i++) //This loop sets a numerical value to the variable month depending on the string value taken in from the file.
    {
    if( strcmp (months,string ) == 0)
    month = i+1;
    }

    if ( year < 80 ) //Years are noted as 98, 01 etc, therefore to give a correct value to the year, must add on 2000 or 1900 as necessary
    year = year + 2000;
    else if ( year > 80 && year < 100)
    year = year + 1900;

    if ( year == 2001 )
    standard = 2451910.5;
    else if ( year == 2002)
    standard = 2451910.5 + 365;

    if( condition == 1 )
    {
    startJulian = julian( day, month, year, hour, minute, second );
    startJulian = startJulian - standard;
    condition++;
    }
    else if ( condition == 2)
    {

    startAverage = presentAverage;
    condition++;
    }
    else if ( condition == 3 )
    {
    endJulian = julian( day, month, year, hour, minute, second );
    endJulian = endJulian - standard;
    endAverage = presentAverage;

    average = ( startAverage + endAverage ) / 2;

    fprintf ( fpWrite,"\n%lf\t%lf\t%lf", startJulian, endJulian, average );
    condition = 2;
    startJulian = endJulian;
    }
    }

    fclose ( fpWrite );
    fclose ( fp );
    }
    return(0);
    }

    //
    // Function to calculate the Julian Date given day, month, year, hour, minute & second
    //

    double julian(double day, double month, double year, double hour, double minute, double second)
    {

    double date; // A running variable calculating the Julian date

    date = day - 32076 +
    1461*( year + 4800 + ( month - 14 ) / 12 ) / 4 +
    367*( month - 2 - ( month - 14 ) / 12 * 12 ) / 12 -
    3*( ( year + 4900 + ( month - 14 ) / 12 ) / 100 ) / 4;

    //By comparison with the original gregorian dates, this function results in being offset from the correct Julian Date by this much
    date = date - 0.118755; //The offset for 2001


    //Add the fractional hours, mins and seconds

    date += ( hour + 12.0 ) / 24.0;
    date += ( minute ) / 1440.0;
    date += ( second ) / 86400.0;

    return date;

    }


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    What data are you 'piping' to this program? The only user input I see in this program is where you ask a user for a filename. Are you just going to pipe locations of files to this program? Read up on how to take in command line arguments in C. This will allow you to pass data to the program. It's very easily done.


Advertisement