Thread: trouble with input redirection - C -

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Melbourne, Victoria, Australia, Australia
    Posts
    5

    Talking trouble with input redirection - C -

    hey guys. i have a program i have to write for uni. i need some help if you have the time. i know this is basic stuff but i missed quite a few lectures, so i am behind. ( as a note arrays are not allowed in this specific homework).


    here my code
    Code:
    #include <stdio.h>
    
    int
    main(int argc, char **argv) {
    int junk, yy, mh, dd, hh, mn, ss;
    double lat, lon, altitude, time;
    scanf("%lf,%lf,%d,%lf,%lf,%4d-%2d-%2d,%2d:%2d:%2d",&lat, &lon, &junk, &altitude, &time, &yy, &mh, &dd, &hh, &mn, &ss);
    printf(" Stage 1\n ========\n GPS trace commences:");
    printf("%04d-%02d-%02d,%02d:%02d:%02d",yy, mh, dd, hh, mn, ss);
    return 0;
    }
    when i input a single line of data it works. but when i ask it to search a text file and print the date/time of the first ROW of proper data, i always get weird values (image below):

    http://i.imgur.com/MWfY9.jpg

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I don't see any code that deals with any of the non-numeric text in your file, so that's why nothing good is being displayed. When you read files, you basically need to account for everything inside of them.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Check the return result of scanf()
    It tells you how successful it was at parsing a line of data.
    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.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Location
    Melbourne, Victoria, Australia, Australia
    Posts
    5
    Whats the best way to tell the program to skip invalid input and keep searching the text file untill it finds the first valid entry. I was thinking a while loop, but i didnt know how to implement it.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
      // do stuff
      // like use sscanf with your above format
      // and checking the return result for success
    }
    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.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Location
    Melbourne, Victoria, Australia, Australia
    Posts
    5
    Thanks salem, but i dont think i am allowed to use arrays. Any other methods you know of?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You should still check the return result for success in what you already have. For anything that isn't good, you can adapt the ideas from this FAQ entry.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't use fgets() if no arrays are allowed.

    Walk through the file, char by char. If the first char in the line of text is not a - (negative sign), or a digit, then use a while(char != '\n') loop to "walk through" the line, and get past the text lines.

    If it IS a negative or a digit, then take each char in and "build" up the total number, stopping when you get to the comma, or a newline. Each digit will need to be * 10 + the new digit, as they are read in, using a loop. (base 10 numbering system).

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I can see that you're stuck with yet another tutor who thinks that teaching people how to run begins with breaking their legs.

    Just what is "don't use arrays" supposed to achieve - lessons in pointlessness and futility.

    Anyway,
    Code:
    if ( scanf(/**/) == 7 ) {
    } else {
      // ditch the rest of the crap on this line
      int ch;
      while ( (ch=getchar()) != EOF && ch != '\n' );
    }
    Oh, and I expect you to replace 7 with some suitable number.
    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.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I can see that you're stuck with yet another tutor who thinks that teaching people how to run begins with breaking their legs.
    Sublime.

    Soma

  11. #11
    Registered User
    Join Date
    Apr 2012
    Location
    Melbourne, Victoria, Australia, Australia
    Posts
    5
    thanks for everything guys. (especially salem). and i totally agree. i think its stupid that we cant use arrays. but oh well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection <
    By bos1234 in forum C Programming
    Replies: 7
    Last Post: 03-27-2012, 06:10 AM
  2. having some trouble reassigning input
    By ecsx00 in forum C Programming
    Replies: 8
    Last Post: 10-09-2011, 02:08 AM
  3. input output redirection
    By chess2009 in forum C Programming
    Replies: 1
    Last Post: 02-26-2011, 06:46 PM
  4. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  5. Help / Loops; input redirection from files
    By Frank_Rye in forum C Programming
    Replies: 10
    Last Post: 10-16-2005, 01:15 AM