Question

C Language program problem.

Suppose that I want to create a program by using command line. User will type a name of file such as "test.data" to access the content of the file.

Program should scan whole file and create a list with 30 position .

You should show the two situation, one is for when scanned space, the other one is when it scanned new line.

Then the other task is printing them out.

test data K 1 1 0llie 2 John 3 Julie 4 Joe 5 Mary 6 Sue 7 Jane 2 apple 3 oranger 2 apple 1 apple 3 oranger 3 pear 2 oranger 2 oranger 3 apple 9 Fred 10 Bill pear

You see there are spaces and next lines in the file which program will scan.

I want to see how to fetch the correct elements when they are divided by space.

Also, I want to see how to fetch the correct elements when they are divided by next line.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

HERE FSCANF WILL READ THE FILE WORD BY WORD SEPERATED BY THE SPACE.

IT CONSIDERS THE NEW LINE ALSO AS A SPACE.

HENCE WE DO NOT HAVE TO WORRY ABOUT THE NEWLINE AND THE SPACE.

CODE::

#include <stdio.h>

#include <stdlib.h>

int main ()

{

                //required variables

                char filename[20],word[50],*list[30]={""};

                int i=0;

                FILE *file ;

                printf("Enter file name :");

                scanf("%s",filename);

                //creating the file pointer

                file = fopen (filename, "r");

                //loop to read word after the space

                while(fscanf(file, "%s",word)==1)

                {

                                //printing current word

                                printf("Current word :: \"%s\"\n",word);

                                //adding word to list

                                list[i++]=word;

                                if(i==30)               break;

                                //end of loop

                }

                //printing report

                printf("\nData copied to list successfully!");

                //closing the file pointer

                fclose(file);

                //exiting funciton with returning 0

                return(0);

}

OUTPUT::

Add a comment
Know the answer?
Add Answer to:
C Language program problem. Suppose that I want to create a program by using command line....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C please Create a program that takes two integers as command line arguments (num, length)...

    In C please Create a program that takes two integers as command line arguments (num, length) and outputs a list of the first length multiples of num. num should be included in the returned array. For example: ./a.out 7 5 -> [7, 14, 21, 28, 35] ./a.out 12, 10 -> [12, 24, 36, 48, 60, 72, 84, 96, 108, 120] ./a.out 17, 6 -> [17, 34, 51, 68, 85, 102] Ma Word starts with a vowel add "yay" to the...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • I am writing a C program that takes either command line arguments or has a file...

    I am writing a C program that takes either command line arguments or has a file passed to it using < on the command line. My question is how do I check to see if there is a file passed using <. I think I need a way to check stdin to see if there is any data present. Thanks

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : i have 15 min to submit the solution please help me   You are given a sequence of n non-zero integers a1,a2,…,an. Determine if the given sequence contains a pair of adjacent elements of the same sign (both negative or both positive). Two elements are called adjacent if they stand next to each other...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : You are given a sequence of n non-zero integers a1,a2,…,an. Determine if the given sequence contains a pair of adjacent elements of the same sign (both negative or both positive). Two elements are called adjacent if they stand next to each other in the sequence. Input The first line contains an integer n...

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

  • 1) Write a C program that displays all the command line arguments that appear on the...

    1) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 2) Write a C program which displays the sum of the command line arguments. Hint: use sscanf to convert the decimal arguments (which are strings) to binary numbers that...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a sequence of n positive integers a1,a2,…,an, where n is even. Swap adjacent elements in the given sequence and print the resulting sequence: a2,a1,a4,a3,a6,a5,… Input The first line contains a positive even integer n (2≤n≤1000) — the length of the sequence. The second line contains n space-separated integers a1,a2,…,an (1≤ai≤1000) — the elements of the sequence. Output Print n...

  • Write a C program that parses a string (command line) and tokenize it by breaking the...

    Write a C program that parses a string (command line) and tokenize it by breaking the string characters into words that are separated by delimiters that can be white spaces (space and/or tab characters). It also must report how many commands in the input. This program will be reused for your next C programming assignment that is a simple shell interpreter/program called "tech shell". % a.out please enter a string: this is a test 1: this 2: is 3: a...

  • Using C, Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and...

    Using C, Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and american2.txt). Each file will have words in random order. The output must be a file called words.txt. Note that you cannot cheat by using Linux commands to do this. It must be done entirely in your C code. File format: apple banana pear . . . Hint: Program will need to utilize double pointers. More Hints: 1. Assume no word is bigger that 50...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT