Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:35:34+00:00 2026-06-17T12:35:34+00:00

I wanted to know how to read data from an unknown source of input,

  • 0

I wanted to know how to read data from an unknown source of input, meaning I don’t know if the user is going to just type a sentence or is he going to give me some text file.
I’ve tried using fscanf since I’ve read it is meant for unformatted input type
this is my code, Im suppose to get some type of input(file or just a sentence (echo bla bla bla) and "int" and print only the "int" first words. The program should be used for piping meaning the command would look like that :
There are 2 ways to ways of using the program:

1.echo "blabla" | myprog 2  (number of words to print)
2.cat input.txt | myprog 2  (number of words to print)

The problematic line is line 16, I tried using fscanf
Thanks!

  1 #include <stdio.h>
  2 #include <ctype.h>
  3 #include <string.h>
  4 #include <stdlib.h>
  5
  6
  7 int main(int argc, char *argv[]){
  8    char *words[32];
  9    int numofwords = atoi(argv[2]);
 10    int i=0;
 11    int len;
 12    char *word = malloc (32 * sizeof(char));
 13    char c;
 14   while (i<=numofwords){
 15    if ((c = getc (argv[1])) != EOF){
 16         fscanf(argv[1],"%s",&word);
 17         len = strlen (word);
 18         words[i] = malloc ((len+1) * sizeof(char));
 19         i++
 20    }
 21    printf(words[i]);
 22   }
 23   return 0;
 24 }
 25
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:35:35+00:00Added an answer on June 17, 2026 at 12:35 pm

    May be I am correctly understood your need.

    I am not rectifying your code but writing my own.

    Below is simple code that read from console: code: main.c

    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>
    int main(int argc, char* argv[]){
        if(argc!=2){
            printf("\n wrong number of argument\n");
            exit(1);
        }
        int numofwords = atoi(argv[1]);
        char buffer[128];
    
        //printf("%d\n",numofwords);
    
        while(numofwords--){
            scanf("%s",buffer);
            printf("%s\n",buffer);
        }
        return 0;
    }   
    

    How does it works:

    ~$ gcc  main.c -o main  
    

    execute:

    :~$ ./main 
    
    wrong number of argument
    :~$ ./main 2
    grijesh
    grijesh
    yourname        
    yourname
    :~$ 
    

    I hope its understood to you. the program simply read (scan) from console and print out to console. The while loop runs for number of time you pass on command line input.

    Now, A text file dumy.txt a input file:

    :~$ cat dumy.txt
    yourname
    myname
    hisname
    hername
    :~$   
    

    Now see what you want to achieve through you code:

    :~$ cat dumy.txt | ./main 2
    yourname
    myname
    :~$   
    

    If you want to pass through echo :

    :~$ echo $'one\ntwo\nthree' | ./main 2
    one
    two
    :~$   
    

    Is this you want?

    If yes:

    What you miss understood that:

    [your code]

    (mistake 1,2)
    Your fsacnf is wrong in two ways:

    fscanf(argv[1],"%s",&word);  
    
    • First argument is argv[1] is char* that is wrong you need to pass FILE* type. As explained in Mr. Oli Charlesworth’s answer.

    • Second you still need to read from stdin. | operator redirects the output from first command to second command.

    (mistake 3, 4, 5, 6)
    By sending echo "blabla" you are just sending a single sting you need to do something like I did (I added \n in echo string for new line also my echo string start with $ so it not print as raw string). echo so that you can read from code according to second argument that is argv[1] not argv[2]. So in your code following line is wrong too.

    int numofwords = atoi(argv[2]);   
    

    I corrected this line in my code.

    and i is initialised to zero i = 0 , in while loop condition is <=, I think it should be <.

    (mistake 7)
    The way you run your code is wrong echo "blabla" | myprog 2 your program not know as mygrog you have to pass complete path. like I did ./main, ./ means current directory.

    this just my view about your question. Read also the answer in comment given by Mr. William Pursell.

    Let me know if you have other doubts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to know about Data Type implementation in PHP so I need a
Just wanted to know how i would replace sitename.com with sitename2.com whenever a user
Wanted to know if there was a way one could query shelveset details from
I wanted to know what is the meaning of <modules runAllManagedModulesForAllRequests=true /> I am
I am planning an application that will read data from the accelerometer 24/7 and
I'm trying to read data in from a binary file and then store in
We are currently migrating to TFS from VSS and I wanted to know what
I am using Python to read in data in a user-unfriendly format and transform
I wanted know how the kernel is providing memory for simple C program .
Wanted to know if someone had a suggestion on code or maybe there's a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.