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

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.
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
(C++) Create a data file and name it "input.txt". manually save 10 integers into the file....
(C++) Create a data file and name it "input.txt". manually save 10 integers into the file. Write a program to read the data and calculate the average of events and odds, separately. Print out the average values.
Write a Python program called “exam.py”. The input file, “input.txt”, is given to you in the...
Write a Python program called “exam.py”. The input file, “input.txt”, is given to you in the Canvas exam instructions. Name your output file “output.txt”. Ensure the program consists of a main function and at least one other function. Ensure the program will read the input file, and will output the following information to the output file as well as printing it to the screen: output the text of the file to screen output the number of words in the file...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT