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 }
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...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
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!
The following code searches a text file for definitions and prints them out. I want to...
The following code searches a text file for definitions and prints them out. I want to create a function that can be called for the given vector and map. The function will either print the results in reverse order, print results without duplicate definitions, or do both. What is the best approach to make the code less repetitive. if (find(keyWords.begin(), keyWords.end(), v[0]) != keyWords.end()) {                        for (auto map_iter = mymap.cbegin(); map_iter != mymap.cend(); ++map_iter)...
The following is for C programming language: I want to scan for initials in a line...
The following is for C programming language: I want to scan for initials in a line of text. my line of text is as follows: 12345 3.5000 a j 12346 4.1000 s p The first number represents the student ID, the second number represents the gpa, the third character represents the first initial and the fourth character represents the last initial of the student. My text file contains these values. The following is my code: fscanf(fp, "%d %c %c", &studentID,...
Using c programming language How do you put data from a text file into a 2d...
Using c programming language How do you put data from a text file into a 2d array For example a text file with names and age: john 65 sam 34 joe 35 sarah 19 jason 18 max 14 kevin 50 pam 17 bailey 38 one 2d array should have all the names from the file and one 2d array should have all the ages and both arrays should be printed out separately and be 3x3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT