Question

C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank...

C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you.

Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename: string or numbers. If the word filename is not found on the command line, the program should let the user know and exit. You can assume that if the word filename is found on the command line, it is always followed by the name of a file and the word string or numbers. YOU MUST USE THE FUNCTION GIVEN. 0 POINTS IF FUNCTION IS MODIFIED OR UNUSED.

/*This function takes a char** pointer and integer. It returns a -1 if the word filename is not found in the char** pointer and any other value otherwise.*/

int find_filename(int n, char **b)

{

int i;

int counter=0; /*equals -1 if didnt find*/

int check=0;

for(i=0;i<n&&check==0;i++)

{

if(strcmp(*b, "filename")==0)

{

check=1;

}

counter++;

b++;

}

if(check!=1)

{

counter=0;

}

return (counter-1);

}

Sample Run 1:

computer$ ./a.out random filename stuff.txt string

Filename: stuff.txt

We're dealing with string info.

Contents of the file:

hello world!

I am here to say that

it is a sunny day.

Sample Run 2:

computer$ ./a.out filename nums.txt numbers

Filename: nums.txt

We're dealing with number info.

Contents of the file:

2

3

4

5

6

7

Sample Run 3:

computer$ ./a.out just numbers words

No filename given. Bye!

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

The complete code is followed by the output snapshot:

#include <stdio.h>
#include <string.h>

/* This function takes a char** pointer and integer. It returns a -1 if the word filename is not
   found in the char** pointer and any other value otherwise. */
int find_filename(int n, char **b)
{
    int i;
    int counter=0; /*equals -1 if didnt find*/
    int check=0;
    for(i=0;i<n&&check==0;i++)
    {
        if(strcmp(*b, "filename")==0)
        {
            check=1;
        }
        counter++;
        b++;
    }
    if(check!=1)
    {
        counter=0;
    }
    return (counter-1);
}

int main(int argc, char **argv)
{
    /* Variables for reading from the file */
    char s[1000];
    int num;

    FILE *file;

    /* Get the filename */
    int index = find_filename(argc, argv);

    if(index == -1)
    {
        printf("No filename given. Bye! ");
        return -1; /* return value denoting didnt find */
    }

    /* If file name found
       index     : filename
       index + 1 : name of the file
       index + 2 : string / numbers
    */
    printf("Filename: %s ", argv[index + 1]);

    file = fopen(argv[index + 1], "r");
    if(file == NULL)
    {
        printf("Error opening file ");
        return -2;
    }

    if(strcmp(argv[index + 2], "string") == 0)
    {
        printf("We're dealing with string info. ");
        printf("Contents of the file: ");
        while(fscanf(file, "%[^ ]", s) > 0)
        {
            printf("%s ", s);
        }
    }
    else
    {
        printf("We're dealing with number info. ");
        printf("Contents of the file: ");
        while(fscanf(file, "%d", &num) > 0)
        {
            printf("%d ", num);
        }
    }

    fclose(file);
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank...
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
  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer...

    T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer is true or false in order to receive credit. #include <stdio.h> #include <string.h> int run_through(int num, char **a) { int i; int check=0; for(i=0;i<num;i++) { printf("%s\n", *(a+i)); if(strcmp(*(a+i), "filename")==0) { check=1; } } return check; } char** find_filename(int n, char **b) { int i; int check=0; for(i=0;i<n;i++) { if(strcmp(*b, "filename")==0) { b++; break; } b++; } return b; } int main(int argc, char **argv)...

  • C Language Program. PLEASE USE DYNAMIC MEMORY AND FOLLOW DIRECTIONS GIVEN. USE FUNCTIONS GIVEN ALSO. Thank...

    C Language Program. PLEASE USE DYNAMIC MEMORY AND FOLLOW DIRECTIONS GIVEN. USE FUNCTIONS GIVEN ALSO. Thank you. NO FFLUSH. Problem 5 (40 points)-Write a program Submit orders.c (Note: you wil be dynamically allocating memory for this problem. The second argument on the command line will let you know how much memory you will be allocating. O points if you do not dynamically allocate) Chef Bartolomeo owns a very busy Italian restaurant. He has recently started accepting orders online and every...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

  • Programming in C. Name this program schwifty.c - This program reads a text file and makes...

    Programming in C. Name this program schwifty.c - This program reads a text file and makes it schwifty, but the user determines the schwiftiness. The user supplies the filename to schwift and a string containing a sequence of the following characters to determine the schwiftiness via command line arguments: L - Left shift each character in a word: hello --> elloh R - Right shift each character in a word: elloh --> hello I - Shift the letters' and digits'...

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

  • GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor...

    GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor declarations #define BUF_SIZE 10 /* global constant buffer size: Generally this would be much larger, but I want to show you that it works in a loop until the entire file is read.*/ // main function int main(int argc, char *argv[]) {    // Variable declarations    int fd;                       // file descripter    char buffer[BUF_SIZE];       // string for...

  • Please write the following code in C++ for Data Structures class. Please follow all the instructions...

    Please write the following code in C++ for Data Structures class. Please follow all the instructions in the picture below. Please write the program with zero warnings. Description: Please write a short program, randomStream, that asks for the name of a file and a number, and produces a files satisfying the following conditions: The file name is the name given by the user (exactly--no added suffixes please) The number of lines in the file is exactly the same as the...

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