Thread: Trying to scan in user input after the '<' not working on gcc.

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    2

    Trying to scan in user input after the '<' not working on gcc.

    I am reading in a csv file, outputting a txt file and also reading in a text file to search for the keys that I have to output into the txt file.

    So basically this is my code.
    /* This is to read in the CSV file" */
    Code:
    while(fscanf(Data_input,"%d; %[^\n]",&weight,num) == 2){         
                    insert(ptr,word,num);
            }
    /*Now I want to read in the search key where read_word is declared as char array of size 500*/

    Code:
    while(scanf("%[^\n]", read_word) == 1){
            printf("%s\n", read_word);
        }
    The command on stdin should look like this after compilation

    ./aout read.csv out.txt < search.txt

    I am unable to read the search.txt. I tried using fscanf and now I am trying to use just scanf to see if anything happens but the command reads out null as there is no such this that exists on argv[4]? I know its not reading in the file since when I print it out it prints null and exits =(
    Last edited by Pdave7; 09-04-2017 at 01:38 AM. Reason: Not in line

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    ./aout read.csv out.txt < search.txt
    Out.txt is read by fopen on argv [1] and using the resulting file handle in fscanf etc
    Search.txt is read simply by doing scanf, because that's now just stdin.
    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
    Registered User
    Join Date
    Aug 2017
    Posts
    2
    Ok so then right now my code is
    Code:
    while(scanf("%[^\n]", read_word) == 1){        printf("%s\n", read_word);
        }


    But is waiting for user input where it should just read it after < search.txt.
    So its not reading in the search.txt for some unknown reason

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You have to skip \n that was not read by [^\n] format to go to the next line
    Code:
    #include<stdio.h>
    
    int main() {
        char read_word[500];
        int res;
        int read =0;
        while((res = scanf("%499[^\n]\n", read_word)) == 1) {
            printf("%s\n", read_word);
            read++;
        }
        printf("Read=%d, res=%d\n", read, res);
    }
    Output
    Code:
    $ ./test < test.txt
    ATHROCYTES
    DISHLIKE
    IRRECOVERABLENESSES
    EMBRITTLEMENTS
    YOUNGS
    OVERPLOTS
    FED
    SQUALLS
    SKINFULS
    CANNAS
    COUNTERGUERILLAS
    FINIAL
    VACATIONERS
    WINTRINESSES
    ...and so on...
    Read=15, res=-1
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. user input bounds not working
    By intex in forum C Programming
    Replies: 2
    Last Post: 11-24-2012, 08:06 PM
  2. Replies: 2
    Last Post: 03-05-2012, 10:35 AM
  3. Replies: 3
    Last Post: 11-01-2010, 08:22 PM
  4. C Function won't scan for user inputted value
    By kevin250 in forum C Programming
    Replies: 10
    Last Post: 10-21-2010, 10:41 PM
  5. User-input not working
    By Serejai in forum C++ Programming
    Replies: 6
    Last Post: 08-22-2004, 06:55 PM

Tags for this Thread