I've been stuck on this problem for a while.
I'm creating a program that that reads in an input file (just a plain .Txt) This input file stores variables in the following format:
x
21
% This is a comment
y
3
And so on. My goal is to read this input file (done), and then store every variable in the file into a global variable in my c program. I.e. Global variable x will have the value 21 and y will have the value 3 in my c file, whilst comments are ignored.
I've thought about this for a while and can't figure out what functions to use. Any suggestions would be appreciated, thanks.
(note that these variables will always have the same names, but the order in which they are presented will differ from input file to input file).
I would suggest you to have a struct with two fields.
struct Foo
{
char var_name;
int var_value;
};
Then you create an array of these structs, with the size of the expected variables in your input file.
struct Foo input_array[n];
Then, as you read your file, you set the struct fields...
for(int i = 0; i < n ; i++){
input_array[i].var_name = input_var_name;
input_array[i].var_value = input_var_value;
}
Afterwards you print the values, and they will already be in order.
First of all, we need the variables to read the values into:
int x, y, z /* etc */;
Now, lets make an array of pointers so that we can easily access the variables:
int *array[] = { &x, &y, &z /* etc */ };
Now, we need a FILE* to access the file:
FILE* fp;
Opening the file:
fp = fopen("filename.txt", "r"); /* 'r' for reading */
Checking if the file opened successfully:
if(fp == NULL)
{
printf("Error opening file");
exit(-1); /* Exit the program */
}
Now, reading the file using fscanf:
int counter = 0; /* For keeping track of the array index */
for(;;) { /* Infinite loop */
int retVal = fscanf(fp, "%d", array[counter]); /* Capture return value of fscanf */
if(retVal == 1) /* Successfully scanned a number */
{
counter++;
}
else if(retVal == 0) /* Failed to scan a number */
{
fscanf(fp, "%*s"); /* Discard a word from the file */
}
else /* EOF */
{
break; /* Get out of the loop */
}
}
Now, printing the scanned data:
int i;
for(i = 0; i < counter; i++)
printf("%d", *array[i]);
and finally, closing the file:
fclose(fp);
Full code, added with #Jongware's suggestions:
int x, y, z /* etc */;
int *array[] = { &x, &y, &z /* etc */ };
char line[1024];
const char *varNames[] = { "x", "y", "z" };
int tmp = -1;
FILE* fp;
fp = fopen("filename.txt", "r"); /* 'r' for reading */
if(fp == NULL)
{
printf("Error opening file");
exit(-1); /* Exit the program */
}
int counter = 0;
for(fgets(line, sizeof(line), stdin)) {
if(line[0] == '%')
continue;
else
{
tmp = -1;
for(int i = 0; i < sizeof(varNames) / sizeof(*varNames); i++)
{
if(strcmp(line, varNames[i]) == 0)
{
tmp = i;
break;
}
}
fgets(line, sizeof(line), stdin);
sscanf(line, "%d", array[tmp]);
}
}
int i;
for(i = 0; i < counter; i++)
printf("%d", *array[i]);
fclose(fp);
All the above code is untested
Related
I'm kinda new to programming so sorry for a stupid question. I've been tasked to read first two lines (numbers) and store them as variable n, and the other one in m. Other numbers bellow those two lines need to be read and store it in a dynamic array (malloc to be exact). Each line has exactly one number. I'm having trouble trying to accomplish this.
Here's my code so far:
#include <stdio.h>
#define MAX_LINE 5
int main() {
FILE* in;
FILE* out;
int n, m, list = (int*)malloc(9 * sizeof(int));
in = fopen("ulazna.txt", "r");
out = fopen("izlazna.txt","w");
if ((in && out) == NULL)
{
printf("Error opening the file...");
return -1;
}
while (fscanf(in, "%d\n", &m) != EOF)
{
fscanf(in, "%d\n", &n);
fscanf(in, "%d\n", &m);
printf("First two: %d %d", n, m);
}
//free(list);
fclose(in);
fclose(out);
return 0;
}
File ulazna.txt has:
3
3
This works as intended but when I write:
3
3
1
2
3
4
5
6
7
8
9
My program prints random numbers from this file.
So, I need to read 3 and 3 into n and m. Then, from 1 to 9 into the array list.
Thanks for help in advance :)
Dynamic array size
If you want to dynamically allocate memory for the list, you would have to count the number of lines in the file first.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n, m;
/* open the file for reading text using "r" */
FILE *file_in = fopen("ulazna.txt", "r");
if (!file_in) { /* same as `file_in == NULL` */
printf("Failed to open file\n");
return EXIT_FAILURE;
}
int lines = 0; /* number of lines */
int c; /* return value of `fgetc` is of type `int` */
/* go through each character in the file */
while ((c = fgetc(file_in)) != EOF) {
/* increment the number of lines after every line break */
if(c == '\n')
++lines;
}
/* reset pointer to start of file */
rewind(file_in);
/* check the return value of `fscanf` */
if (fscanf(file_in, "%d\n", &n) != 1 ||
fscanf(file_in, "%d\n", &m) != 1) {
printf("File contains invalid line\n");
return EXIT_FAILURE;
}
printf("First two: %d %d\n", n, m);
/* no need to cast `malloc`, because it returns `void *` */
int *list = malloc(lines * sizeof(int));
for (int i = 0; i < lines - 2; ++i) {
/* check the return value of `fscanf` */
if (fscanf(file_in, "%d\n", &list[i]) != 1) {
printf("File contains invalid line\n");
return EXIT_FAILURE;
}
printf("%d\n", list[i]);
}
free(list); /* always free memory allocated by malloc */
fclose(file_in);
return EXIT_SUCCESS;
}
Fixed array size
If you already know that the number of lines in the file is going to be 11, the array has a length of 9 and there is no need to dynamically allocate memory. But because your assignment requires it, I have implemented malloc.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n, m;
/* open the file for reading text using "r" */
FILE *file_in = fopen("ulazna.txt", "r");
if (!file_in) { /* same as `file_in == NULL` */
printf("Failed to open file\n");
return EXIT_FAILURE;
}
/* check the return value of `fscanf` */
if (fscanf(file_in, "%d\n", &n) != 1 ||
fscanf(file_in, "%d\n", &m) != 1) {
printf("File contains invalid line\n");
return EXIT_FAILURE;
}
printf("First two: %d %d\n", n, m);
/* the array has a fixed size of 9 */
const int size = 9;
int *list = malloc(size * sizeof(int));
/*
* If you do not have to use malloc, remove the
* above line and uncomment the following line:
*
* int list[size];
*/
for (int i = 0; i < size; ++i) {
/* check the return value of `fscanf` */
if (fscanf(file_in, "%d\n", &list[i]) != 1) {
printf("File contains invalid line or has to few lines\n");
return EXIT_FAILURE;
}
printf("%d\n", list[i]);
}
fclose(file_in);
return EXIT_SUCCESS;
}
I have coded a formula to read the txt file, store it in array(1D) and then reading the array to calculate the moving average(2D). Program ask the user to input two values (k1 & k2) and calculate the moving average for every value from k1 to k2 (basically to find out the best value)
Following is the code
#include<stdlib.h>
#define MAX_FILE_NAME 100
#define MAXCHAR 1000
int main()
{
FILE *fp;
int count = 0,k1=0,k2=0,k=0; // Line counter (result)
int buy[k2][count],sell[k2][count];
char filename[MAX_FILE_NAME];
char c; // To store a character read from file
// Get file name from user. The file should be
// either in current folder or complete path should be provided
printf("Enter file name or full path: ");
scanf("%s", filename);
printf("Enter the minimum rolling period for calculation : \n");
scanf("%d", &k1);
printf("Enter the maximum rolling period for calculation : \n");
scanf("%d", &k2);
// Open the file
fp = fopen(filename, "r");
// Check if file exists
if (fp == NULL)
{
printf("Could not open file %s", filename);
return 0;
}
// Extract characters from file and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n') // Increment count if this character is newline
count = count + 1;
// Close the file
fclose(fp);
//printf("The file %s has %d lines\n", filename, count);
FILE *myFile;
myFile = fopen(filename, "r");
//read file into array
float numberArray[count];
int i;
if (myFile == NULL){
printf("Error Reading File\n");
exit (0);
}
for (i = 0; i < count; i++){
fscanf(myFile, "%f,", &numberArray[i]);
}
fclose(myFile);
for (k=k1;k<=k2;k++)
{
float n;
float data[count],mag[k2][count];
double avg,sum;
for (i=0;i<k-1;i++)
{
mag[k][i-1]=0;
sum=sum+numberArray[i];
}
for(i=k-1;i<=count;i++)
{
mag[k][i-1]=avg;
sum=sum+numberArray[i]-numberArray[i-k];
avg = sum/k;
}
// for(i=0;i<=count;i++)
// {
// printf("MA[%d][%d] = %0.2lf ",k,i,mag[k][i]);
// if (i%3==0)
// printf("\n");
// }
}
for(k=k1;k<=k2;k++)
{
for(i=0;i<=count;i++)
printf("MA[%d][%d] = %0.2lf ",k,i,mag[k][i]);
}
}
}
Now when I am trying to print mag[k][i] values outside the for loop, it is showing error 'mag' undeclared. But when I am putting the print command inside the loop (comment out portion in the code), it works fine.
UPDATED CODE AFTER FOLLOWING COMMENTS (STILL NOT WORKING THOUGH)
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_FILE_NAME 100
#define MAXCHAR 1000
int main()
{
FILE *fp;
int count,k1,k2,k; // Line counter (result)
char filename[MAX_FILE_NAME];
char c; // To store a character read from file
// Get file name from user. The file should be
// either in current folder or complete path should be provided
printf("Enter file name or full path: ");
scanf("%s", filename);
printf("Enter the minimum rolling period for calculation : \n");
scanf("%d", &k1);
printf("Enter the maximum rolling period for calculation : \n");
scanf("%d", &k2);
// Open the file
fp = fopen(filename, "r");
// Check if file exists
if (fp == NULL)
{
printf("Could not open file %s", filename);
return 0;
}
// Extract characters from file and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n') // Increment count if this character is newline
count = count + 1;
// Close the file
fclose(fp);
//printf("The file %s has %d lines\n", filename, count);
/****************
File opening and reading section
*****************************************************/
FILE *myFile;
myFile = fopen(filename, "r");
//read file into array
float numberArray[count];
int i;
if (myFile == NULL){
printf("Error Reading File\n");
exit (0);
}
for (i = 0; i < count; i++){
fscanf(myFile, "%f,", &numberArray[i] );
}
fclose(myFile);
/***********************************************
Calculation of Moving Average and storing it in array
******************************************/
int buy[k2][count],sell[k2][count];
float mag[k2][count];
for (k=k1;k<=k2;k++)
{
float data[count];
double avg,sum;
for (i=1;i<k;i++)
{
mag[k][i-1]=0;
sum=sum+numberArray[i];
}
for (i=k-1;i<=count;i++)
{
mag[k][i-1]=avg;
sum=sum+numberArray[i]-numberArray[i-k];
avg = sum/k;
}
// for(i=0;i<=count;i++)
// {
// printf("MA[%d][%d] = %0.2lf ",k,i,mag[k][i]);
// if (i%3==0)
// printf("\n");
// }
}
for (k=k1;k<=k2;k++)
{
for (i=0;i<=count;i++)
{
printf("MA[%d][%d] = %0.2lf ",k,i,mag[k][i]);
}
}
}
The problem boils down to this: the scope of mag is limited to the inside of the for loop:
for (k = k1; k <= k2; k++)
{
...
int mag[k2][count];
...
}
// mag is out of scope here
// therefore following line won't compile:
printf("%d", mag[0][0]);
You need to declare mag outside the for loop for example like this:
int mag[k2][count];
for (k = k1; k <= k2; k++)
{
...
}
printf("%d", mag[0][0]);
...
Beware: there are other problems within your code, mentioned in the comments.
int main() {
FILE *fp = fopen("fileA.txt", "r"); /* read file */
int i = 0;
char name[200][100];
char goods[200][100];
char qty[200][100];
char temp[200][100];
int x = 0;
int result;
while (!feof(fp)) {
fscanf(fp, "%[^,] , %[^,] , %s " , name[i], item[i], qty[i]); /*get file content and store in array */
if (strcmp(item[i], "Football") == 0) { /* only select Football */
temp[x][x] = qty[i];
if (x > 0) {
if (strcmp(temp[x][x], temp[x + 1][x + 1]) > 0) { /*compare who has more football qty */
result = x; /*output the person who have more football*/
}
}
x = x + 1;
}
}
printf("%s is team leader in class.\n", name[result]);
fclose(fp);
getchar();
return 0;
}
Hi all, I don't know why the result not correct.
I want to find out who has more football and print out his/her name.
Seems something wrong on if (strcmp(temp[x], temp[x + 1]) > 0)
I am not clearly on using pointer and address.
the content in the text file are:
Alice,Eating,001
Kitty,Football,006
Ben,Swimming,003
May,Football,004
And I expect the result is :
Kitty is team leader in class.
Thank you.
There are multiple problems in your code:
you do not test if the file is properly open.
you cannot properly parse a file with while (!feof(fp)) {. You should iterate for as long as fscanf() returns 3, or preferably read the input line by line and parse it with sscanf().
you do not tell fscanf() the maximum number of characters to store into the destination arrays. This may cause undefined behavior for invalid input.
you do not increment i for each line read. Every line of input overwrites the previous one.
you do not check if there are more than 200 lines. Undefined behavior in this case.
Your test to find the football fan with the highest quantity is broken: no need for a 2D array here, just keep track of the current maximum and update it when needed.
Here is a modified version:
#include <stdio.h>
int main() {
FILE *fp = fopen("fileA.txt", "r"); /* read file */
char buf[300];
char name[200][100];
char goods[200][100];
char qty[200][100];
int i, qty, max_qty = 0, result = -1;
if (fp == NULL) {
fprintf(stderr, "cannot open file\n");
return 1;
}
for (i = 0; i < 200; i++) {
if (!fgets(buf, sizeof buf, fp))
break;
if (sscanf(buf, " %99[^,], %99[^,],%99s", name[i], item[i], qty[i]) != 3) {
fprintf(stderr, "invalid input: %s\n", buf);
break;
}
if (strcmp(item[i], "Football") == 0) { /* only select Football */
qty = atoi(qty[i]);
if (result == -1 || qty > max_qty) {
result = i; /*store the index of the person who have more football */
}
}
}
if (result < 0)
printf("no Football fan at all!\n");
else
printf("%s is team leader in class with %d in Football.\n", name[result], max_qty);
fclose(fp);
getchar();
return 0;
}
Above Code is not clear as what you want to do in this code block
if ( strcmp(temp [x], temp [x+1]) > 0 ){ /* when matches, accessing temp[x+1] results in undefined behaviour */
result = x;
}
also why char *temp[200][100]; as to store qty[i], char *temp is enough or you can take char temp[200][100];
Here is somewhat better one as requirement is not clear.
int main() {
FILE *fp= fopen("fileA.txt","r"); /* read file */
if(fp == NULL) {
/* not exist.. write something ?? */
return 0;
}
char name [200][100],goods[200][100],qty[200][100],temp[200][100];
int x = 0,result = 0, i = 0;
while ((fscanf(fp, "%[^,] , %[^,] , %s " , name[i], goods [i], qty[i])) == 3) {
if (strcmp(goods[i] , "Football") == 0){
strcpy(temp[x],qty[i]);
if ( strcmp(temp [x], temp [x+1]) > 0 ) { /* UB ? */
result = x;
x+=1;
}
}
}
printf("%s is team leader in class. \n", name[result]);
fclose(fp);
getchar();
return 0;
}
I have a text file which contains a list of words in a precise order.
I'm trying to create a function that return an array of words from this file. I managed to retrieve words in the same order as the file like this:
char *readDict(char *fileName) {
int i;
char * lines[100];
FILE *pf = fopen ("francais.txt", "r");
if (pf == NULL) {
printf("Unable to open the file");
} else {
for (i = 0; i < 100; i++) {
lines[i] = malloc(128);
fscanf(pf, "%s", lines[i]);
printf("%d: %s\n", i, lines[i]);
}
fclose(pf);
return *lines;
}
return "NULL";
}
My question is: How can I return an array with random words from the text file; Not as the file words order?
The file looks like this:
exemple1
exemple2
exemple3
exemple4
Reservoir sampling allows you to select a random number of elements from a stream of indeterminate size. Something like this could work (although untested):
char **reservoir_sample(const char *filename, int count) {
FILE *file;
char **lines;
char buf[LINE_MAX];
int i, n;
file = fopen(filename, "r");
lines = calloc(count, sizeof(char *));
for (n = 1; fgets(buf, LINE_MAX, file); n++) {
if (n <= count) {
lines[n - 1] = strdup(buf);
} else {
i = random() % n;
if (i < count) {
free(lines[i]);
lines[i] = strdup(buf);
}
}
}
fclose(file);
return lines;
}
This is "Algorithm R":
Read the first count lines into the sample array.
For each subsequent line, replace a random element of the sample array with probability count / n, where n is the line number.
At the end, the sample contains a set of random lines. (The order is not uniformly random, but you can fix that with a shuffle.)
If each line of the file contains one word, one possibility would be to open the file and count the number of lines first. Then rewind() the file stream and select a random number, sel, in the range of the number of words in the file. Next, call fgets() in a loop to read sel words into a buffer. The last word read can be copied into an array that stores the results. Rewind and repeat for each word desired.
Here is a program that uses the /usr/share/dict/words file that is typical on Linux systems. Note that if the number of lines in the file is greater than RAND_MAX (the largest number that can be returned by rand()), words with greater line numbers will be ignored. This number can be as small as 32767. In the GNU C Library RAND_MAX is 2147483647.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_WORD 100
#define NUM_WORDS 10
int main(void)
{
/* Open words file */
FILE *fp = fopen("/usr/share/dict/words", "r");
if (fp == NULL) {
perror("Unable to locate word list");
exit(EXIT_FAILURE);
}
/* Count words in file */
char word[MAX_WORD];
long wc = 0;
while (fgets(word, sizeof word, fp) != NULL) {
++wc;
}
/* Store random words in array */
char randwords[NUM_WORDS][MAX_WORD];
srand((unsigned) time(NULL));
for (size_t i = 0; i < NUM_WORDS; i++) {
rewind(fp);
int sel = rand() % wc + 1;
for (int j = 0; j < sel; j++) {
if (fgets(word, sizeof word, fp) == NULL) {
perror("Error in fgets()");
}
}
strcpy(randwords[i], word);
}
if (fclose(fp) != 0) {
perror("Unable to close file");
}
/* Display results */
for (size_t i = 0; i < NUM_WORDS; i++) {
printf("%s", randwords[i]);
}
return 0;
}
Program output:
biology's
lists
revamping
slitter
loftiness's
concur
solemnity's
memories
winch's
boosting
If blank lines in input are a concern, the selection loop can test for them and reset to select another word when they occur:
/* Store random words in array */
char randwords[NUM_WORDS][MAX_WORD];
srand((unsigned) time(NULL));
for (size_t i = 0; i < NUM_WORDS; i++) {
rewind(fp);
int sel = rand() % wc + 1;
for (int j = 0; j < sel; j++) {
if (fgets(word, sizeof word, fp) == NULL) {
perror("Error in fgets()");
}
}
if (word[0] == '\n') { // if line is blank
--i; // reset counter
continue; // and select another one
}
strcpy(randwords[i], word);
}
Note that if a file contains only blank lines, with the above modification the program would loop forever; it may be safer to count the number of blank lines selected in a row and skip until some reasonable threshold is reached. Better yet to verify that at least one line of the input file is not blank during the initial line-count:
/* Count words in file */
char word[MAX_WORD];
long wc = 0;
long nonblanks = 0;
while (fgets(word, sizeof word, fp) != NULL) {
++wc;
if (word[0] != '\n') {
++nonblanks;
}
}
if (nonblanks == 0) {
fprintf(stderr, "Input file contains only blank lines\n");
exit(EXIT_FAILURE);
}
I need some help with my C project:
I need to write a c program who receives 2 parameters:
1) The name of a text file(infile) which is in the same catalog
2) A number k>0
And creates 2 new files,outfile1 & outfile 2 as:
Outfile 1: k,2*k,3*k…. character of infile
Outfile 2: k,2*k,3*k…..line of infile
Example:
INFILE
Abcdefg
123456
XXXXXX
01010101
OUTFILE 1:
Cf25XX101
OUTFILE 2:
XXXXXX
I wrote some code ,but its not working. Any ideas?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** read_lines(FILE* txt, int* count) {
char** array = NULL;
int i;
char line[100];
int line_count;
int line_length;
*count = 0;
line_count = 0;
while (fgets(line, sizeof(line), txt) != NULL) {
line_count++;
}
rewind(txt);
array = malloc(line_count * sizeof(char *));
if (array == NULL) {
return NULL;
}
for (i = 0; i < line_count; i++) {
fgets(line, sizeof(line), txt);
line_length = strlen(line);
line[line_length - 1] = '\0';
line_length--;
array[i] = malloc(line_length + 1);
strcpy(array[i], line);
}
*count = line_count;
return array;
}
int main(int argc, char * argv[]) {
char** array = NULL;
FILE* file = NULL;
const char* filename = NULL;
int i;
int line_count;
int k;
char c;
printf("ENTER ONE PHYSICAL NUMBER\n");
do{
if(k>0)
scanf("%d",&k);
else{
printf("ENTER ONE PHYSICAL NUMBER\n");
scanf("%d",&k);
}
}while(k<=0);
file = fopen("LEIT.txt", "rt");
if (file == NULL) {
printf("CANT OPEN FILE %s.\n", filename);
return 1;
}
array = read_lines(file, &line_count);
printf("ARRAY:\n");
for (i = 0; i < line_count; i++) {
printf("[%d]: %s\n", (i+1), array[i]);
}
printf("CALCULATING OUTFILE1 AND OUTFILE2\n");
printf("OUTFILE1:\n");
for(i=0;i<line_count;i++){
c=i*k;
printf("%c\n",array[c]);
}
printf("WRITING OUTFILE1 COMPLETE!\n");
printf("OUTFILE2:\n");
for(i=0;i<line_count;i++){
c=i*k;
printf("%c\n",array[c]);
}
printf("WRITING OUTFILE2 COMPLETE!\n");
return 0;
}
My actual problem is calculate and write into files (outfile1 and outfile2) the result...
You need to close file after finishing reading/writing it with fclose.
You can create and write strings to a file using fopen with correct mode.
You can output formatted string to a file by using fprintf.
It seems that you don't want to print the 0th character/line, so in the last for loop, i should start from 1 (or start from 0 but add 1 later).
array[c] is a string, not a character. So when printing it, you should use %s specifier instead of %c.
It is not a good idea using char as count in later for loops unless you know input file will be very short. signed char can only count to 127 before overflow (unsigned char can count to 255). But if you have a very long file, for example thousands of lines, this program would not work properly.
array is malloced in function char** read_lines(FILE* txt, int* count). After finish using it, you need to dealloc, or free it by calling
for (i = 0; i < line_count; i++) {
free(array[i]);
}
and followed by free(array). This avoids memory leakage.
Modified code is here. In the following code, char c is not used. This is the part where you process output files, and before return 0; in main function.
printf("CALCULATING OUTFILE1 AND OUTFILE2\n");
printf("OUTFILE1:\n");
// Since we finished using LEIT.txt, close it here.
fclose(file);
// Mode: "w" - Write file. "+" - Create if not exist.
// You can lso use "a+" (append file) here if previous record need to be preserved.
FILE *out1 = fopen("OUTFILE1.txt", "w+");
FILE *out2 = fopen("OUTFILE2.txt", "w+");
if ((out1 == NULL) || (out2 == NULL)) {
printf("CANT CREATE OUTPUT FILES.\n");
return 1;
}
// Out file 1.
unsigned int count = k;
for (i = 0; i < line_count; i++){
while (count < strlen(array[i])) {
// This just prints to stdout, but is good for debug.
printf("%c", array[i][count]);
// Write to the file.
fprintf(out1, "%c", array[i][count]);
// Calculate c for next char.
count += k + 1;
}
// Before go to next line, minus string length of current line.
count -= strlen(array[i]);
}
printf("\n");
printf("WRITING OUTFILE1 COMPLETE!\n");
// Close file.
fclose(out1);
// Out file 2.
printf("OUTFILE2:\n");
for (i = 1;i < line_count / k; i++){
count = i * k;
// This just prints to stdout, but is good for debug.
printf("%s\n", array[count]);
// Write to the file.
fprintf(out2, "%s\n", array[count]);
}
printf("WRITING OUTFILE2 COMPLETE!\n");
//Close file.
fclose(out2);
// dealloc malloced memory.
for (i = 0; i < line_count; i++) {
free(array[i]);
}
free(array);