Like Tree4Likes
  • 4 Post By Salem

trouble with input redirection - C -

This is a discussion on trouble with input redirection - C - within the C Programming forums, part of the General Programming Boards category; hey guys. i have a program i have to write for uni. i need some help if you have the ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    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
    32,824
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    32,824
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    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
    32,824
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,407
    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, 07:10 AM
  2. having some trouble reassigning input
    By ecsx00 in forum C Programming
    Replies: 8
    Last Post: 10-09-2011, 03: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, 02:59 AM
  5. Help / Loops; input redirection from files
    By Frank_Rye in forum C Programming
    Replies: 10
    Last Post: 10-16-2005, 02:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21