Question

In: Computer Science

For c language. I want to read a text file called input.txt for example, the file...

For c language.

I want to read a text file called input.txt for example, the file has the form.

4

hello

goodbye

hihi

goodnight

where the first number indicates the n number of words while other words are separated by newlines.

I want to store these words into a 2D array so I can further work on these.

and

there are fewer words in the word file than specified by the number in the first line of the file, then you must print an error message and return a non-zero value from main.

You must only read the first n words, you can ignore all words after n and should continue with the program as normal.

Thanks!

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.


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


int main()
{
   FILE * file;
   file = fopen("input.txt" , "r");
   printf("Reading file...\n");
   if (!file){
       perror("ERROR opening file! File not found!!!\n");
       return 0;
   }

   char s1[100];
   int count=0;
   int possibleCount = 0;
   fscanf(file, "%d", &possibleCount);

   char words[possibleCount][100];
   while (fscanf(file, "%s", s1)!=EOF)
   {
       strcpy(words[count],s1);
       count++;
       if(count==possibleCount){
           break;
       }
   }

   if(count<possibleCount){
       printf("Error!!! Less amount of words in file than expected!!!");
       return 1;
   }
   printf("Content of the 2D array is \n");
   int i;
   for(i=0;i<count;i++){
       printf("%s\n",words[i]);
   }
   return 0;
}

input.txt

4
hello
goodbye
hihi
goodnight
nice
pizza
dominoz

output


Related Solutions

C language problem. Suppose I open I file and read a integer 24. And I want...
C language problem. Suppose I open I file and read a integer 24. And I want to store them into a array byte []. The answer should be byte[0] = 0x9F. How can I do that?
so i want to read in a file. just to keep it simple, the text file...
so i want to read in a file. just to keep it simple, the text file has student name and the grade, separated by a space. let's say there are 192 student's name and I want to read them in and made them into a Student /object class, which has a constructor, student(string name, int grade). individually creating them seems pain in the ass. reading in should work for any number of names. So how do I do this in...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt"...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt" (you will have to provide your own when debugging). The text file that is to be opened is formatted a certain way, namely, there is always one integer, one character (representing an operation), another integer, and then a new line character, with spaces between each item. A sample text file is provided below. theNumbers.txt 144 + 26 3 * 18 88 / 4 22...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now encrypt the text file using Caesar Cipher (Use key value as 5). Display the contents of the updated file. #include <iostream> using namespace std; int main() { // write code here }
How would I read only the first line of text file into C++ For instance, the...
How would I read only the first line of text file into C++ For instance, the first line of a text file is: 5 (space) 5. I need to read these numbers into a row variable and a column variable. I am just not sure how to do it. I can only use the #include header. I can't use any other header. The project mentions using redirected input in Visual Studio for our text file.
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
Write a C++ programm which reads an input file named 'input.txt' content of input.txt are not...
Write a C++ programm which reads an input file named 'input.txt' content of input.txt are not known to you copies content of input file into an output file named 'output.txt' contents of output.txt should be exactly the same as contents of input.txt the user should be given a choice to select input and output file in any folder remember to check that input file opened successfully Please add comments next to the code, so I can learn from the solution....
Write a C++ pgm which 1.reads an input file named 'input.txt' 2.content of input.txt are not...
Write a C++ pgm which 1.reads an input file named 'input.txt' 2.content of input.txt are not known to you 3.copies content of input file into an output file named 'output.txt' 4.contents of output.txt should be exactly the same as contents of input.txt 5.the user should be given a choice to select input and output file in any folder 6.remember to check that input file opened successfully
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
Write a program in Java that reads an input text file (named: input.txt) that has several...
Write a program in Java that reads an input text file (named: input.txt) that has several lines of text and put those line in a data structure, make all lowercase letters to uppercase and all uppercase letters to lowercase and writes the new lines to a file (named: output.txt).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT