Question

C programming language question. Supposed that there is a txt file with content "AA101 ABC ADC...

C programming language question.

Supposed that there is a txt file with content "AA101 ABC ADC 2019-11-24", I want to read the information by blank space.

When program read the file, I want the program only print out ABC and ADC.

Please show the code.

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

//Rate my solution

//Comment if any doubts

CODE:

#include<stdio.h>
#include<stdlib.h>
main(){
   char c,*str;
   int index,flag;
   // to open a file we need a file pointer
   FILE *fp;
   // openinig the file f.txt you change f.txt to your filename.txt
   // r is the mode of the file i.e., to read the contents of file f
   fp=fopen("f.txt","r");
   do{
       // getting each character in the file
       c=fgetc(fp);
       // this is the memory allocation for the string we want to print
       str=(char *)malloc(100*sizeof(char));
       // index is the index for storing in the str
       index=0;
       // flag is used to check whether it has any non aplhabets
       flag=1;
       // this loop is for checking the string upto space or newline or End of file
       while(c!=' ' && c!=EOF && c!='\n'){
           // if the character in the file is not an alphabet then falg is zero
           if(!isalpha(c))
               flag=0;
           else
               str[index++]=c; //storing the aplhabet in the str and incrementing the index
           // it works as
           // str[index]=c;
           // index=index+1;  
           //getting next character int the file      
           c=fgetc(fp);  
       }
       //the last value of every string is \0 it means the string has ended
       str[index]='\0';
       // flag 1 says that it has only alphabets
       // index 0 means empty string so index should be not zero
       if(flag==1 && index!=0)
           printf("%s\n",str);
       // clearing the space created by the str for new string
       free(str);
      
   }while(c!=EOF);
}

Code(Screenshot):

File (f.txt):

Output:

Add a comment
Know the answer?
Add Answer to:
C programming language question. Supposed that there is a txt file with content "AA101 ABC ADC...
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
  • I need help with this program. Im trying to use C++ programming language. I want to...

    I need help with this program. Im trying to use C++ programming language. I want to include my ifstream SimpleCal6.txt and my outFile. CS 317 Calculator Program Write a program to input three values, two floats and an operator print out what were read and the resulting evaluation. The operators to handle are +, -, *, / and ^. The floats are one or more digits in length. The last input is sentinel value 0 + 0; Read a float,...

  • In C Programming Language, write a simple program to read one text file and print to...

    In C Programming Language, write a simple program to read one text file and print to screen all its text. Make use of the stderr and exit program statements should there not be only one text file identified on the command line. If successful then before fclose of the text file, use the fputs program statement to write a line "72 degrees Sunny light wind. Nice!\n". print the return code of fputs (should be zero). close and exit. Test program...

  • Python programming question Using an adjacency matrix: Take a txt file and have the program read...

    Python programming question Using an adjacency matrix: Take a txt file and have the program read it and output the contents into an adjacency matrix. - The txt file would have the size of the matrix and a list of names. - Print the filled matrix of the names - After each iteration, the program will ask, 'continue'? - Show how to swap names for the next iteration(Program would take an input of two names and swap them) Example: input:...

  • programming language is C++. Please also provide an explanation of how to create a file with...

    programming language is C++. Please also provide an explanation of how to create a file with the given information and how to use that file in the program Let's consider a file with the following student information: John Smith 99.0 Sarah Johnson 85.0 Jim Robinson 70.0 Mary Anderson 100.0 Michael Jackson 92.0 Each line in the file contains the first name, last name, and test score of a student. Write a program that prompts the user for the file name...

  • C++ programming language Write a program that asks the user for a file name. The file...

    C++ programming language Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in...

  • Please provide C language code no c++ ,txt file also needed with comment Finish task 5...

    Please provide C language code no c++ ,txt file also needed with comment Finish task 5 Task5: Breadth First Search (15 pts) · Write a program to read the graph information from the file and traverse the graph using BFS algorithm as introduced in lecture. The input for each algorithm is an undirected unweighted connected graph stored in a local file using an adjacency list. Following is the example of the input file (graph.txt) and the graph First line is...

  • C++ programming help. The only change you have to make is in current program, where it...

    C++ programming help. The only change you have to make is in current program, where it says "your code goes here", do not make any changes to any other files, they are just there to show you. This code should be written for arbitrary data, not specifically for this sequence of data. The only place you need to write a code is marked YOUR CODE GOES HERE. You have been supplied a file with data in it (data2.txt). The line...

  • Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space....

    Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space. Write a C++ program that reads the two numbers from each line and prints how many numbers are prime between them. Your program must have a user defined function that takes one number as input parameter and detects whether its prime or not. Show your output in a file named B2.txt using the following format: Sample Input: 3 17 42 91 Sample Output: 6...

  • Description: Save a text document to disk based on a name and content provided by the...

    Description: Save a text document to disk based on a name and content provided by the user. Purpose: This application provides experience with user input and interaction in the Console, writing files to disk, working with exceptions, and writing programs in C#/.NET. Requirements: Project Name: Document Target Platform: Console Programming Language: C# Documentation: Types and variables (Links to an external site.) (Microsoft) Console.ReadLine Method () (Links to an external site.) (Microsoft) Strings (C# Programming Guide) (Links to an external site.)...

  • I am writing a program in c programming and it is supposed to do the following using mc9s12dg256.h microcontroller When...

    I am writing a program in c programming and it is supposed to do the following using mc9s12dg256.h microcontroller When both DIP switches #8 and #1 are high, turn on all LEDS. When both DIP switches #8 and #1 are low, turn off all LEDs When DIP switch #8 is high and #1 is low, turn on all the even numbered LEDs. When DIP switch #1 is high and #8 is low, turn on all the odd numbered LEDs. Your...

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