Question

Write a program in C that takes a file name as the only argument on the...

Write a program in C that takes a file name as the only argument on the command line, and prints out the number of 0-bits and 1-bits in the file


int main ( int argc , char * argv [] ) {

// Check if the user gave an argument , otherwise print " ERROR : no argument "

// Check if the file can be read , otherwise print " ERROR : can ’t read "

// Otherwise , use a loop to read each character of the file // For each character count the number of 0: s and 1: s .

// Print out the total number of 0: s and 1: s in the file , as a decimal number

}

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

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
FILE *filePtr;
char ch;
int countZero = 0;
int countOne = 0;
  
if(argc < 2) // 1st argument is: argv[0] = ./a.out, argv[1] = fileName.txt
printf("ERROR : no argument");
else
{
// number of argument is correct; check if file can be read
filePtr = fopen(argv[1], "r");
if (filePtr == NULL)
{
printf("ERROR : file can't be read %s\n", argv[1]);
exit(0);
}
  
// file can be read
ch = fgetc(filePtr);
while(ch != EOF)
{
if(ch == '0')
countZero++;
else if(ch == '1')
countOne++;
  
ch = fgetc(filePtr);
}
fclose(filePtr);
  
// display the counts of zeros and ones in the file
printf("Number of 0's in the file: %d\n", countZero);
printf("Number of 1's in the file: %d\n", countOne);
}

return 0;
}

*************************************************************** SCREENSHOT ************************************************************

CONSOLE OUTPUT:

Number of Os in the file: 32 Number of ls in the file: 34 ... Program finished with exit code o Press ENTER to exit console

INPUT FILE: (input.txt)

main.c input.txt 1 1 111100001101000001111111110001222888 2 779001222199905500111117889033221110 3 44522008000011100440411569

Add a comment
Know the answer?
Add Answer to:
Write a program in C that takes a file name as the only argument on the...
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
  • 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...

  • 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...

  • 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)...

  • If you already answer this question, please skip, thanks C Programming. Fill in ... This program...

    If you already answer this question, please skip, thanks C Programming. Fill in ... This program will be called with one command line argument that contains a string followed by an asterisk and an integer. Print out the string as many time as indicated by the integer. For example, when called as prog Hi*3, you print HiHiHi. Hint: Look for the '*' starting from the back of the string. len = strlen(arg) gives you the string length. When you have...

  • 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...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • need this in c programming and you can edit the code below. also give me screenshot...

    need this in c programming and you can edit the code below. also give me screenshot of the output #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define LINE_SIZE 1024 void my_error(char *s) { fprintf(stderr, "Error: %s\n", s); perror("errno"); exit(-1); } // This funciton prints lines (in a file) that contain string s. // assume all lines has at most (LINE_SIZE - 2) ASCII characters. // // Functions that may be called in this function: // fopen(), fclose(), fgets(), fputs(),...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • I am writing a program in C++, which requires me to read an input text file...

    I am writing a program in C++, which requires me to read an input text file using command line argument. However, I am using xcode on my Macbook to write C++ program, and use terminal instead of command. How do you use int main(int argc, char** argv[]) to read an input file. My professor requires us not to hard code the text file name like .open("example.txt"); Thank you!

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