So I am trying to read a matrix from a file (I am sure there are better ways to do it than how I am doing it). I had a hard time figuring out how to read each word from the file (meaning each entry of the matrix) so decided to read each line and use something I found here in stackexchange called strtok.
The inside of my main() looks like this
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
#include <time.h>
#include <string.h>
int main(){
FILE *f;
int nmatrix=3;
int nmax=3;
int something;
int number, count, count2, i, j;
srand(time(NULL));
size_t len = 0;
ssize_t read;
char * line = NULL;
char * pch;
int ia;
int (*B)[nmatrix][nmatrix] = malloc(nmatrix * nmatrix * sizeof(int));
while(nmatrix<=nmax){
// Creation of Matrix
f = fopen("matrix.txt", "w");
for(count = 1; count <= nmatrix; count ++){
for(count2 = 1; count2 <= nmatrix; count2 ++){
number = rand()%9;
fprintf(f, "%s%d ", " ", number);
}
fprintf(f, "%s\n", " ");
}
fclose(f);
// Reading Matrix
f = fopen("matrix.txt", "r");
i=0;
while((read = getline(&line, &len, f)) != -1) {
printf("%s\n", line);
pch = strtok(line," ,.-");
j=0;
while (pch != NULL & j<nmatrix){
ia= (int)*pch-48;
*B[i][j]= ia;
pch = strtok(NULL, " ,.-");
j=j+1;
}
i=i+1;
}
fclose(f);
nmatrix=nmatrix+1;
}
return 0;
}
The first output in terminal is if the line *B[i][j]= ia; is erased, and the second with it. The first ouput reads all the lines in the file and the second doesnt read the last. Why? (The output looks different cuz the matrices are generated at random).
Thanks in advance
I am fairly new to everything, specially pointers so if anything is not used correctly I'd appreciate the comment.
You had a couple of issues. You were using & instead of && (not really affecting it, but still wrong) and you had your pointer wrong in the assignment. I changed the output to display what is stored in the matrix, instead of the read line.
*B[i][j]= ia; needs to be (*B)[i][j]= ia;
// Reading Matrix
f = fopen("matrix.txt", "r");
i=0;
while((read = getline(&line, &len, f)) != -1) {
pch = strtok(line," ,.-");
j=0;
while (pch != NULL && j<nmatrix){
printf("\n%d %d :", i, j);
ia= (int)*pch-48;
(*B)[i][j]= ia;
printf("%d\n", (*B)[i][j]);
pch = strtok(NULL," ,.-");
j=j+1;
}
i=i+1;
}
fclose(f);
You have a lot of errors:
your string to break the numbers os the matrix are not inserted when you create the matrix: " ,.-". These must be inserted to be used in strtok function.
should be && instead of & in while statement
you have an extra } at the end of file.
both whiles to create the matrix should start at 0 and go to nmatrix with < symbol.
you should initialize the string line with "", not null.
int main(){
FILE *f;
int nmatrix=3;
int nmax=3;
int number, count, count2, i, j;
srand(time(NULL));
size_t len = 0;
ssize_t read;
char * line = "";
char * pch;
int ia;
int (*B)[nmatrix][nmatrix] = malloc(nmatrix * nmatrix * sizeof(int));
while(nmatrix<=nmax){
// Creation of Matrix
f = fopen("matrix.txt", "w");
for(count = 0; count < nmatrix; count ++){
for(count2 = 0; count2 < nmatrix; count2 ++){
number = rand()%9;
fprintf(f, "%s%d ", " ", number);
}
fprintf(f, "%s\n", " ");
}
fclose(f);
// Reading Matrix
f = fopen("matrix.txt", "r");
i=0;
while((read = getline(&line, &len, f)) != -1) {
printf("%s\n", line);
pch = strtok(line," ");
j=0;
while (pch != NULL && j < nmatrix){
ia= (int)*pch-48;
*B[i][j]= ia;
pch = strtok(NULL, " ");
j=j+1;
}
i=i+1;
}
fclose(f);
nmatrix=nmatrix+1;
}
return 0;
}
Related
in the text file it'll say something like this:
12 4 23 76
7 3 12 54
1 54 2 67
...
int arr[26];
int arr2[26];
int arr3[26];
int main(){
fp = fopen ("myfile.txt", "r");
while (fgets(store, sizeof(store), fp)){
//I tried using scanf but I couldn't get it to work
printf("%s", store); //prints out a line
}
}
I know that the 'store' has the string that I want to work with.
how can I grab the integers from 'store' that are separated by space and put them into an array?
so I would want
arr[0]=12 arr[1]=4 arr[2]=23 arr[3]=76,
arr2[0]=7 arr2[1]=3 arr2[2]=12 arr2[2]=65,
arr3[0]=1 arr3[1]=54 arr3[2]=2 arr3[3]=67
you can use strtok() and atoi() function if string format is guaranteed.
here's draft of code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int arr[3][26];
char store[100];
char *split;
int i, j;
int main(){
i = 0;
FILE* fp = fopen ("myfile.txt", "r");
while (fgets(store, sizeof(store), fp)){
printf("%s", store); //prints out a line
split = strtok(store, " ");
j = 0;
while(split) {
arr[i][j++] = atoi(split);
split = strtok(NULL, " ");
}
i++;
}
for(i = 0; i < 3; i++) {
for(j = 0; j < 4; j++) {
printf("%2d ", arr[i][j]);
}
puts("");
}
}
I hope that you can utilize this code for what you want.
-> I fixed code which is able to run.
you can modify this code because I tested and it worked.
I'm not sure which point you struggled, but I suggest you to check out how to use those functions in web.
the code posted by ChangHo is pretty accurate barring a few corrections.
Since the data contained in the text file, seems to contain blank lines, the primary fix is to ensure that these lines are ignored.
I notice in your comment, that when you try printing the vales after "strtok", you notice 0s.That's because the value of the variable "j" has been incremented....And is yet to be assigned a new value.....
arr[i][j++] = atoi(split);// j has been incremented
split = strtok(NULL, " ");
printf("%d\n", arr[i][j]);//displays 0,since "j" is yet to be assigned a value
All in all, here is the updated version of the code posted....
Please see the comments......
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int arr[3][26];
char store[100];
int i = 0;
int j = 0;
char *split;
int main(){
FILE *fp;
fp = fopen ("test.txt", "r");
while (fgets(store, sizeof(store), fp)){
if(strlen(store) == 1)//When fgets returns just the newline character
continue; //skip the line
printf("%s", store); //prints a line
split = strtok(store," ");
j = 0;
while(split != NULL){
arr[i][j] = atoi(split);
split = strtok(NULL," ");
printf("%d\n", arr[i][j]);//print value in array before j is incremented
j++;
}
arr[i][j] = '\0';//terminate the sub array with the NULL character
i++;
}
//print the result
for(int i = 0;i<3;++i){
for(int j = 0;arr[i][j] != '\0';++j)
printf("%d ",arr[i][j]);
putchar('\n');
}
fclose(fp);
}
I have a .csv file that reads like:
SKU,Plant,Qty
40000,ca56,1245
40000,ca81,12553.3
40000,ca82,125.3
45000,ca62,0
45000,ca71,3
45000,ca78,54.9
Note: This is my example but in reality this has about 500,000 rows and 3 columns.
I am trying to convert these entries into a 2D array so that I can then manipulate the data. You'll notice that in my example I just set a small 10x10 matrix A to try and get this example to work before moving on to the real thing.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *getfield(char *line, int num);
int main() {
FILE *stream = fopen("input/input.csv", "r");
char line[1000000];
int A[10][10];
int i, j = 0;
//Zero matrix
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
A[i][j] = 0;
}
}
for (i = 0; fgets(line, 1000000, stream); i++) {
while (j < 10) {
char *tmp = strdup(line);
A[i][j] = getfield(tmp, j);
free(tmp);
j++;
}
}
//print matrix
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
printf("%s\t", A[i][j]);
}
printf("\n");
}
}
const char *getfield(char *line, int num) {
const char *tok;
for (tok = strtok(line, ",");
tok && *tok;
tok = strtok(NULL, ",\n"))
{
if (!--num)
return tok;
}
return 0;
}
It prints only "null" errors, and it is my belief that I am making a mistake related to pointers on this line: A[i][j] = getfield(tmp, j). I'm just not really sure how to fix that.
This is work that is based almost entirely on this question: Read .CSV file in C . Any help in adapting this would be very much appreciated as it's been a couple years since I last touched C or external files.
It looks like commenters have already helped you find a few errors in your code. However, the problems are pretty entrenched. One of the biggest issues is that you're using strings. Strings are, of course, char arrays; that means that there's already a dimension in use.
It would probably be better to just use a struct like this:
struct csvTable
{
char sku[10];
char plant[10];
char qty[10];
};
That will also allow you to set your columns to the right data types (it looks like SKU could be an int, but I don't know the context).
Here's an example of that implementation. I apologize for the mess, it's adapted on the fly from something I was already working on.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Based on your estimate
// You could make this adaptive or dynamic
#define rowNum 500000
struct csvTable
{
char sku[10];
char plant[10];
char qty[10];
};
// Declare table
struct csvTable table[rowNum];
int main()
{
// Load file
FILE* fp = fopen("demo.csv", "r");
if (fp == NULL)
{
printf("Couldn't open file\n");
return 0;
}
for (int counter = 0; counter < rowNum; counter++)
{
char entry[100];
fgets(entry, 100, fp);
char *sku = strtok(entry, ",");
char *plant = strtok(NULL, ",");
char *qty = strtok(NULL, ",");
if (sku != NULL && plant != NULL && qty != NULL)
{
strcpy(table[counter].sku, sku);
strcpy(table[counter].plant, plant);
strcpy(table[counter].qty, qty);
}
else
{
strcpy(table[counter].sku, "\0");
strcpy(table[counter].plant, "\0");
strcpy(table[counter].qty, "\0");
}
}
// Prove that the process worked
for (int printCounter = 0; printCounter < rowNum; printCounter++)
{
printf("Row %d: column 1 = %s, column 2 = %s, column 3 = %s\n",
printCounter + 1, table[printCounter].sku,
table[printCounter].plant, table[printCounter].qty);
}
// Wait for keypress to exit
getchar();
}
There are multiple problems in your code:
In the second loop, you do not stop reading the file after 10 lines, so you would try and store elements beyond the end of the A array.
You do not reset j to 0 at the start of the while (j < 10) loop. j happens to have the value 10 at the end of the initialization loop, so you effectively do not store anything into the matrix.
The matrix A should be a 2D array of char *, not int, or potentially an array of structures.
Here is a simpler version with an allocated array of structures:
#include <stdio.h>
#include <stdlib.h>
typedef struct item_t {
char SKU[20];
char Plant[20];
char Qty[20];
};
int main(void) {
FILE *stream = fopen("input/input.csv", "r");
char line[200];
int size = 0, len = 0, i, c;
item_t *A = NULL;
if (stream) {
while (fgets(line, sizeof(line), stream)) {
if (len == size) {
size = size ? size * 2 : 1000;
A = realloc(A, sizeof(*A) * size);
if (A == NULL) {
fprintf(stderr, "out of memory for %d items\n", size);
return 1;
}
}
if (sscanf(line, "%19[^,\n],%19[^,\n],%19[^,\n]%c",
A[len].SKU, A[len].Plant, A[len].Qty, &c) != 4
|| c != '\n') {
fprintf(stderr, "invalid format: %s\n, line);
} else {
len++;
}
}
fclose(stream);
//print matrix
for (i = 0; i < len; i++) {
printf("%s,%s,%s\n", A[i].SKU, A[i].Plant, A[i].Qty);
}
free(A);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int *width;
int *height;
int row;
int column;
int character;
int count;
int pictureit;
double i = 0;
FILE *fp;
char file[50];
char line[25]; // assume each line has max 25 characters
printf("What file should we pull from: ");
scanf("%s", file);
//read file using File pointer
fp = fopen(file, "r");
// read the first line in the file
fgets(line, sizeof(line), fp);
width = strtok(line,"x");
height = strtok(NULL, "/0");
// read all the future lines in the file excluding the first
while (fgets(line, sizeof(line), fp)) {
row = strtok(line, ",");
column = strtok(NULL, ",");
character = strtok(NULL, ",");
count = strtok(NULL, "/0");
if(i < count) {
**printf("%s", pictureit[row][column] = character);**
i++;
}
}
fclose(fp);
return 0;
}
I'm pulling in a file with this kind of setup
75x53
0,36,.,1
0,37,M,1
1,32,.,1
1,33,:,1
1,34,A,1
1,35,M,2
1,37,O,1
1,38,:,1
2,23,.,1
2,24,:,1
2,25,A,1
2,26,M,5
I've been brainstorming for a while. How would I go about displaying this on the console? It obviously needs to go into a 2d array. The program needs to know the height and width of the array to display a space or character in that spot.
PS: This program will display a picture in the console when finished. The "** **" is where I am working.
You could dynamically allocate a 2d array with the right dimensions (according to your first line), then fill it up with the data from your file and finally print it with two nested for loops.
EDIT: Basically, you would do:
//...
//Create the dynamic array
char ** array = malloc(sizeof(char) * height);
int i;
for(i = 0; i < height; i++)
array[i] = malloc(sizeof(char) * width);
// Fill your array here (put different chars in it) ...
//Print it
int x,y;
for(y = 0; y < height; y++)
{
for(x = 0; x < width; x++)
printf("%c ", array[y][x]);
printf("\n");
}
//Free the array
for(i = 0; i < height; i++)
free(array[i]);
free(array);
I voluntarily omitted the part where you check the return value of the malloc (whether it's NULL or not), but you should definitely add it.
Normally I wouldn't do this, but I felt the need to do a scanning exercise:
int main(void)
{
char fn[100];
fprintf(stdout, "Enter file name:");
fflush(stdout);
int result = fscanf(stdin, " %99s", fn);
if (1 != result)
{
fprintf(stderr, "Reading the file's name failed.\n");
exit(EXIT_FAILURE);
}
{
size_t width= 0;
size_t height 0;
FILE * pf = fopen(fn, "r");
if (NULL == pf)
{
fprintf(stderr, "Opening file '%s' failed.\n", fn);
exit(EXIT_FAILURE);
}
{
result = fscanf(pf, " %zux%zu", &width, &height);
if (2 != result)
{
fprintf(stderr, "Reading width and/or heigth from file '%s' failed.\n", fn);
exit(EXIT_FAILURE);
}
{
char (*pa)[width][height] = calloc(1, sizeof *pa);
if (NULL == pa)
{
perror("calloc() failed");
exit(EXIT_FAILURE);
}
{
size_t number_of_rows = width * height;
fprintf(stderr, "Trying to read %zu data rows.\n", number_of_rows);
for (size_t row = 0; row < number_of_rows; ++row)
{
size_t x, y;
char c;
int i;
result = fscanf(pf, " %zu,%zu,%c,%d", &x, &y, &c, &i);
if (4 != result)
{
fprintf(stderr, "Reading data (#%zu) row from '%s' failed.\n", row, fn);
exit(EXIT_FAILURE);
}
/* Add check here to avoid accessing the array out-of-bounds! */
(*pa)[x][y] = c;
}
}
{
for (size_t row = 0; row < width; ++row)
{
for (size_t column = 0; column < height; ++column)
{
fprintf(stdout, "%c", (*pa)[row][column]);
}
fprintf(stdout, "\n");
}
}
free(pa);
}
}
fclose(pf);
}
return EXIT_SUCCESS;
}
Also I am curious about the picture to be printed ... ;-)
I have a program that uses word search. I have a data file which contains the puzzle and the words. What can i implement into my program so that it reads the file and stores the letters present in it as an array?
Example of the data file (it is called testdata):
h e l l o a c d
f g b w o r l d
h a c c v b n a
e q b x n t q q
y e h n c a q r
hello
world
hey
I want to store all the letters in a 2-d array.
Also, I need to store all the words in a 1-dimensional array.
The maximum number of rows of columns or rows that AxA square of letters that is possible in a data file is 25. So, I believe that I should declare an array of that size for the letter and then write them into that array.
I just can't figure out how to read them into that array. There is a space after each letter in the array and no spaces in the words so I think that might be helpful when putting the letters in one array and words in another.
Given your question, and your input, there are a few questions, but in the interest of time, for now, I have made some assumptions about the dimensions of the array, i.e. that it is not necessarily square (as implied by columns or rows that AxA square). The actual data sample disagrees, so I wrote a routine that counts everything as it goes. The letter array is simply an array of arrays, but since it is stored in sequential memory, it just looks like one long array. The strings are each in there own location as well. In any case, this code should illustrate enough to get you on the right track...
#include <ansi_c.h>
#include <stdio.h>
void GetFileContents(char *file, int *nWords, int *lw, int *r, int *c);
void allocMemoryStr(int numStrings, int max);
void allocMemoryLtr(int numStrings, int max);
void freeMemoryStr(int numStrings);
void freeMemoryLtr(int numletters);
#define FILENAME "c:\\dev\\play\\_puzzle.txt"
char **letters;
char **strings;
int main()
{
int longest, cnt, wCount, rows, cols, i;
char line[260];
FILE *fp;
char *buf=0;
GetFileContents(FILENAME, &wCount, &longest, &rows, &cols);
allocMemoryStr(wCount, longest); //for strings
allocMemoryLtr(rows*cols, 1); //for strings
//read file into string arrays
fp = fopen(FILENAME, "r");
cnt=0;
for(i=0;i<rows;i++)
{
fgets(line, 260, fp);
buf = strtok(line, " \n");
while(buf)
{
strcpy(letters[cnt], buf);
buf = strtok(NULL, " \n");
cnt++; //use as accurate count of words.
}
}
cnt=0;
while(fgets(line, 260, fp)) //get remainder of lines into strings
{
//[EDIT]removed fgets()
buf = strtok(line, " \n");
while(buf)
{
strcpy(strings[cnt], buf);
buf = strtok(NULL, " \n");
cnt++; //use as accurate count of words.
}
}
fclose(fp);
freeMemoryStr(wCount);
freeMemoryLtr(rows*cols);
return 0;
}
void GetFileContents(char *file, int *nWords, int *lw, int *r, int *c)
{
char line[260];
FILE *fp;
char *buf=0;
char temp[80];
int wc=0, rc=0, cc=0, ck=0;
fp = fopen(FILENAME, "r");
while(fgets(line, 260, fp))
{
rc++;
buf = strtok(line, " \n");
while(buf)
{
strcpy(temp, buf); // word handler
if(strlen(temp) > 1)
{
wc++;
rc--; //
}
else if(strlen(temp) == 1) //leter handler
{
cc++;
(cc>ck)?(ck=cc):(cc=cc);
}
buf = strtok(NULL, " \n");
}
cc = 0;
}
fclose(fp);
*nWords = wc;
*r = rc;
*c = ck;
}
void allocMemoryStr(int numStrings, int max)
{
int i;
strings = calloc(sizeof(char*)*(numStrings+1), sizeof(char*));
for(i=0;i<numStrings; i++)
{
strings[i] = calloc(sizeof(char)*max + 1, sizeof(char));
}
}
void allocMemoryLtr(int numletters, int max)
{
int i;
letters = calloc(sizeof(char*)*(numletters+1), sizeof(char*));
for(i=0;i<numletters; i++)
{
letters[i] = calloc(sizeof(char)*max + 1, sizeof(char));
}
}
void freeMemoryStr(int numStrings)
{
int i;
for(i=0;i<numStrings; i++)
if(strings[i]) free(strings[i]);
free(strings);
}
void freeMemoryLtr(int numletters)
{
int i;
for(i=0;i<numletters; i++)
if(letters[i]) free(letters[i]);
free(letters);
}
I would parse the file line by line and char by char looking for what i need. In the example (which is untested), i hold three counters to help filling the arrays correctly.
char letters[25][25];
char words[10][25]
int letters_x_pos = 0; // Row counter
int letters_y_pos = 0; // Column counter
int words_pos = 0;
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25; j++) {
letters[i][j] = '\0';
}
}
const char *line;
while (line = some_read_function()) {
if (!(strlen(line) > 1)) {
continue;
}
if (line[1] == ' ') {
// Line contains letters
const char *letter = line;
while (*letter != '\0') {
if (*letter == ' ' || *letter == '\n' || *letter == '\r') {
continue;
}
else {
letters[letters_x_pos][letters_y_pos++] = *letter;
}
if (letters_y_pos == 25) {
// Maximum reached
break;
}
letter++;
}
// Increment row counter and reset column counter
letters_x_pos++;
letters_y_pos = 0;
if (letters_x_pos == 25) {
// Maximum reached
break;
}
}
else {
// Line contains word
strncpy(words[words_pos++], line, 25);
if (words_pos == 25) {
// Maximum reached
break;
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int i = 0, len1=0, len2=0;
int BUFSIZE = 1000;
char* string1[20];
char* string2[20];
FILE *fp1 = fopen("input1.txt", "r");
FILE *fp2 = fopen("input2.txt", "r");
if ((fp1 == 0)||(fp2 == 0)){
fprintf(stderr, "Error while opening");
return 0;
}
string1[i] = (char*)malloc(BUFSIZE);
string2[i] = (char*)malloc(BUFSIZE);
while (fgets(string1[i], BUFSIZE, fp1)) {
i++;
len1+=strlen(string[i]);
string1[i] = (char*)malloc(BUFSIZE);
len1+=strlen(string1[i]);
}
i = 0;
while (fgets(string2[i], BUFSIZE, fp2)) {
i++;
string2[i] = (char*)malloc(BUFSIZE);
}
printf("Output: \n");
srand(time(NULL));
int j = rand()%i;
int k = (j+1)%i;
fflush(stdout);
printf("%d - %s %d -%s", j, string1[j], k, string1[k]);
printf("%d - %s %d -%s", j, string2[j], k, string2[k]);
printf("\n");
printf("%d", len1);
int x;
for(x = 0; x<i; x++){
free(string1[x]);
free(string2[x]);
}
scanf("%d", x);
fclose(fp1);
fclose(fp2);
return 0;
}
Thanks to user1807597's help, I finally realize reading two strings and storing into arrays. But I still have trouble in getting the length of the array, I try to put len+=strlen(string[i]); in the while loop, but the compiler breaks when debugging. Someone knows why? Thank you!
You have two main choices for the length of the array.
You make it big enough that you don't think you'll ever use more entries.
enum { MAX_LINES = 16 * 1024 };
char *lines[MAX_LINES];
size_t num_lines = 0;
You do dynamic allocation of the array.
char **lines = 0;
size_t max_lines = 0;
size_t num_lines = 0;
...
if (num_lines >= max_lines)
{
size_t new_lines = max_lines * 2 + 2;
char **space = realloc(lines, new_lines * sizeof(*space));
if (space == 0)
...deal with out of memory...
lines = space;
max_lines = new_lines;
}
Both systems work. Dynamic allocation is a little more fiddly the first few times you do it, but you don't run into problems until you run out of memory, and it takes a long time to run out of memory these days. Fixed allocation may run out of space if you guess wrong, and nominally wastes memory if you only deal with small files. The 'wasted' memory is usually very small these days.
Which is better depends on the scale of the problems you expect your program to deal with. If they'll be small, a fixed but generous allocation is sensible and easier. If they'll be large, the dynamic allocation is more sensible.
I have modified the code.I think now it's ok.You have wrong counting in the first while loop.And i++ should come after len1+=strlen(string[i]);.In the last scanf statement scanf("%d", x);,you have missed the operator '&'.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int i = 0, len1 = 0, len2 = 0;
int BUFSIZE = 1000;
/*
you have too few lines here,
If my file contains more than 20 lines,tragedy will occur!
*/
char* string1[20];
char* string2[20];
FILE* fp1 = fopen("input1.txt", "r");
FILE* fp2 = fopen("input2.txt", "r");
if ((fp1 == 0) || (fp2 == 0))
{
fprintf(stderr, "Error while opening");
return 0;
}
string1[i] = (char*)malloc(BUFSIZE);
string2[i] = (char*)malloc(BUFSIZE);
while (fgets(string1[i], BUFSIZE, fp1)!=NULL)
{
/*
i++;
*/
len1 += strlen(string1[i]);
i++;
string1[i] = (char*)malloc(BUFSIZE);
}
i = 0;
while (fgets(string2[i], BUFSIZE, fp2))
{
i++;
string2[i] = (char*)malloc(BUFSIZE);
}
printf("Output: \n");
srand(time(NULL));
int j = rand() % i;
int k = (j + 1) % i;
fflush(stdout);
printf("%d - %s %d -%s", j, string1[j], k, string1[k]);
printf("%d - %s %d -%s", j, string2[j], k, string2[k]);
printf("\n");
printf("%d", len1);
int x;
for (x = 0; x < i; x++)
{
free(string1[x]);
free(string2[x]);
}
/*
scanf("%d", x);
*/
scanf("%d",&x);
fclose(fp1);
fclose(fp2);
return 0;
}