Question

In: Computer Science

C Programming How do you read in a text file line by line into a an...

C Programming

How do you read in a text file line by line into a an array.

example, i have the text file containing:

line 1: "qwertyuiop"

line 2: "asdfghjkl"

line 3: "zxcvbnm"

Then in the resulting array i get this:

array:"qwertyuiopasdfghjklzxcvbnm"

Solutions

Expert Solution

//C program for gcc (GCC) 9.3.0 and above

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
   FILE *fp = fopen("input.txt","r");
   char *line = NULL;
    size_t len = 0;
    ssize_t read;
   if(fp==NULL)
   {
       printf("Error Opening File!!");
   }
   int i;
   char *arr = (char *)malloc(sizeof(char)*5000);
   while ((read = getline(&line, &len, fp)) != -1) {
       strcat(arr, line);
    }
    printf("%s",arr);
}


Related Solutions

Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
Computer Science - Java Programming How do you read a text file and store three different...
Computer Science - Java Programming How do you read a text file and store three different pieces of information in the database when the given text file contains this info.: 12345 Computer Science Bob Stone 23456 Art James G. Ocean? These are written in the format as ID Class Name. I was going to take the three different pieces of information by separating them by spaces, but the number of spaces is random and I don't know how to adjust...
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.
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
C Programming Number output lines of a text file as it prints to the terminal Do...
C Programming Number output lines of a text file as it prints to the terminal Do not use fgets or fopen It is possible to open file without fopen using SyS commands such as open and RDONLY Example 1 Line 1 2 Line2
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...
1) How do you read the contents of a text file that contains whitespace characters as...
1) How do you read the contents of a text file that contains whitespace characters as part of its data? 2) What capability does the fstream data type provide that the ifstream and ofstream data types do not? 3) What header file do you need to include in a program that performs file operations? 4) What data type do you use when you want to create a file stream object that can write data to a file? 5) What data...
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 }
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{    ...
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