Segmentation error due to allocation issues? - c

Hi so im preety new to coding and I have recently hit a brickwall
This program has a segmentation fault debug and I think it has to do with allocating space for the buffer array or the input file , as those kept popping up as solutions in my search for an answer.
If you know what I did wrong I would appreciate if you told me rather than giving me the solution. Also Im completely lost on how to allocate memory to anything , so it would be great if someone explained how to allocate memory in context to the code.
It is supposed to read a file structured like this
2
3 4
3 4
where 2 = how many files are downloading, 3 = the Kb/s of file( each line represents a file) , and 4 = time remaining until done. The program is supposed to output a file out that has the time needed for all the things to download(mind you when a file finishes the speed goes up to others)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
FILE *in = fopen("download.in", "r");
FILE *out = fopen("download.out", "w");
int i, n, j, a, k, o;
char buffer[100];
fgets(buffer, 10, in);
sscanf(buffer, "%d", &n);
int tn[n];
int xn[n];
i = 0;
while (i < n) {
fgets(buffer, 100, in);
sscanf(buffer, "%d %d", &tn[i], &xn[i]);
++i;
}
for (i = 0; i < n; ++i){
for (j = i + 1; j < n; ++j){
if (tn[i] > tn[j]){
a = tn[i];
tn[i] = tn[j];
tn[j] = a;
a = xn[i];
xn[i] = xn[j];
xn[j] = a;
}
}
}
i = 1;
float b ;
k = 0;
o = 1;
while(o < n){
if(xn[o] == xn[o - 1]){
k = tn[o] + tn[o - 1]
;
}
else{
b = (tn[o] * xn[o]) / (tn[o] + tn[o - 1] + k);
k = 0;
}
++o;
}
fprintf( out, "%f", b );
fclose(in); fclose(out);
return 0;
}
I know the answer it gives is not accurate but I want to fix the segmentation fault first then deal with that
gdb r then gdb bt returns this
(gdb) r
Program received signal SIGSEGV, Segmentation fault.
_IO_fgets (buf=0x7fffffffeb30 "", n=10, fp=0x7fffffffeac0) at iofgets.c:50
50 iofgets.c: No such file or directory.
(gdb) bt
#0 _IO_fgets (buf=0x7fffffffeb30 "", n=10, fp=0x7fffffffeac0) at iofgets.c:50
#1 0x0000000000400766 in main () at main.c:11
codeblocks says the code is ok and the site I am trying to give this says there is a segmentation fault aswell.

Here is code with segfault fixed. I had to (1) specify absolute path and (2) change the loop to a for loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
FILE *in = fopen("/home/developer/CLionProjects/untitled4/download.in", "r");
FILE *out = fopen("/home/developer/CLionProjects/untitled4/download.out", "w");
int i, n, j, a, k, o;
char buffer[100];
fgets(buffer, 10, in);
sscanf(buffer, "%d", &n);
int tn[n];
int xn[n];
for (i = 0; i < n; i++) {
if (!fgets(buffer, 100, in)) break;
if (sscanf(buffer, "%d %d", &tn[i], &xn[i]) < 2) break;
}
for (i = 0; i < n; ++i) {
for (j = i + 1; j < n; ++j) {
if (tn[i] > tn[j]) {
a = tn[i];
tn[i] = tn[j];
tn[j] = a;
a = xn[i];
xn[i] = xn[j];
xn[j] = a;
}
}
}
i = 1;
float b;
k = 0;
o = 1;
while (o < n) {
if (xn[o] == xn[o - 1]) {
k = tn[o] + tn[o - 1];
fprintf(out, "Some number: %d\n", k);
} else {
b = (tn[o] * xn[o]) / (tn[o] + tn[o - 1] + k);
k = 0;
fprintf(out, "Some number: %d\n", k);
}
++o;
}
fclose(in);
const char *text = "Write this to the file";
fprintf(out, "Some text: %s\n", text);
fclose(out);
int c;
FILE *file;
file = fopen("/home/developer/CLionProjects/untitled4/download.out", "r");
if (file) {
while ((c = getc(file)) != EOF)
putchar(c);
fclose(file);
}
return 0;
}

Related

Print 500 time string with concatated unique random number in text file in C

i want to write code that will print 500 line with this string:
if(strstr(""token: XXXXXXXXXX"", token_read)){ token_num = X; }
So XXXXXXXXXX must be a unique (non-repeated) random number, and X must be number incremented by 1. And text file will be look like this:
if(strstr(""token: 1312312412"", token_read)){ token_num = 1; }
if(strstr(""token: 5829572542"", token_read)){ token_num = 2; }
etc.
But my code don't working, and if it would work code don't generate a unique numbers. So in double-array must be 500x10 random numbers
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char x[500][11];
char b[100];
int a, z;
for(z = 0; z < 499; z++){
x[z] = rand(10);
}
FILE *fptr;
fptr = fopen("a.txt", "rb+");
if(fptr == NULL){
fptr = fopen("a.txt", "wb");
if (fptr == NULL)
return -1;
else
for(a = 2; a < 500; a++){
snprintf(b, sizeof(b), "if(strstr(""token: %d"", token_read)){ token_num = a; }", x[a])
fputs(b, fptr);
}
}
fclose(fptr);
}
And sorry for my bad English.
Before this post closes and for giving me such entertainment, here is a small piece of your puzzle that is missing
char x[500][11];
char b[100];
int a, z, i;
for(z = 0; z < 500; z++)
{
for(i=0; i<10;i++)
{
x[z][i] = '0' + rand() % 10;
}
x[z][10] = '\0';
printf("%s \n", x[z]);
}
This does not check for uniqueness. You will have to figure that out along with everything else.

How to create a program that prints out a histogram of letters from a txt file in c

So the program is supposed to take in a txt file with only lowercase letters and no white space. For example "aabbhello". The program will then print out a histogram to the command line:
a: 2
b: 2
h: 1
e: 1
l: 2
o: 1
what I currently have should work but always gives me weird outputs:
void file_histogram(char *filename)
{
FILE *fptr;
int fsize;
fptr = fopen(filename, "r");
if(fptr == NULL) {
printf("File did not open \n");
}
fseek(fptr, 0, SEEK_END);
fsize = ftell(fptr);
char content[fsize];
rewind(fptr);
char c;
int count1 = 0;
while(fscanf(fptr, "%c", &c) == 1) {
content[count1] = c;
count1++;
}
char name[fsize];
int count[26];
int i;
for (i = 0; i < fsize; i++) {
//printf("%d", i);
name[i] = content[i];
}
int length = strlen(name);
for(i = 0; i < length; i++) {
count[i] = 0;
}
for(i = 0; i < length; i++) {
count[content[i] - 'a']++;
}
for(i = 0; i < 26; i++) {
if(count[i] != 0) {
printf("%c : %d \n", i + 'a', count[i]);
}
}
fclose(fptr);
}
when I compile the program I print out letters that aren't even in the txt file at some ridiculous high frequency.
a : 1
i : 1781796
l : 2
m : 1390299937
n : 32767
w : 1
That's what I got when my txt file had "william" as the content. I honestly don't know what's wrong. Does it have to do with the memory maybe? Any help would be appreciated!

Find combination of groups and letters

I have to find a combination of groups of letters, second letter in first group should be the same as first letter in second group etc.
For example, solution for this group: AA, CB, AC, BA, BD, DB
is this: CB, BD, DB, BA, AA, AC
I have this code so far, it works, but if there is a lot of groups, it takes ages to compute. I need to make it more efficient.
In the input file, there's this input
10
C D
B C
B B
B B
D B
B B
C A
A B
B D
D C
My code
#include <stdio.h>
#include <stdlib.h>
void permutation(char group[][2], int buffer, int sum) {
int i, j;
char temp;
if (buffer == sum && group[1][1] == group[sum][2]) {
for (i = 1; i < sum; i++)
if (group[i][2] != group[i+1][1]) break;
if (i == sum) {
FILE *output;
output = fopen("output.txt", "a");
for (j = 1; j <= sum; j++) {
fprintf(output, "%c %c\n", group[j][1], group[j][2]);
}
exit(1);
}
} else {
for (i = buffer; i <= sum; i++) {
temp = group[buffer][1];
group[buffer][1] = group[i][1];
group[i][1] = temp;
temp = group[buffer][2];
group[buffer][2] = group[i][2];
group[i][2] = temp;
permutation(group, buffer + 1, sum);
temp = group[buffer][1];
group[buffer][1] = group[i][1];
group[i][1] = temp;
temp = group[buffer][2];
group[buffer][2] = group[i][2];
group[i][2] = temp;
}
}
}
int main() {
FILE *input;
input = fopen("input.txt", "r");
int sum, i;
fscanf(input, "%d", &sum);
char group[sum][2];
for (i = 1; i <= sum; i++) {
fscanf(input, "%s", &group[i][1]);
fscanf(input, "%s", &group[i][2]);
}
permutation(group, 1, sum);
}
EDIT So I have made some changes in my program (thanks to your help, I'm very new to programming so I'm sorry for mistakes), I use permutations no more and I'm just finding path. It works well, but now my input has 100000 groups and it takes a lot of time once again (about 2 hours and I need to make it done in 1 hour in maximal). I will probably have to do that in other way once again xD Any ideas ?
#include <stdio.h>
int find(char group[][2], int buffer, int sum, int path[]) {
int i, j;
for (i = 0; i < sum; i++) {
for (j = 0; j < buffer; j++)
if (path[j] == i)
break;
if (buffer == 0 ||
(group[path[buffer-1]][1] == group[i][0] && buffer == j)) {
printf("%d\n", buffer); // just for me to know what program is currently computing
path[buffer] = i;
find(group, buffer + 1, sum, path);
if (path[sum-1] != 0)
return;
}
}
}
int main() {
FILE *input = fopen("input.txt", "r");
if (input != NULL) {
int sum, i;
fscanf(input, "%d", &sum);
char group[sum][2];
int path[sum];
for (i = 0; i < sum; i++)
fscanf(input, " %c %c", &group[i][0], &group[i][1]);
for (i = 0; i < sum;i++)
path[i] = 0;
find(group, 0, sum, path);
FILE *output = fopen("output.txt", "a");
for (i = 0; i < sum; i++)
fprintf(output, "%c %c\n", group[path[i]][0], group[path[i]][1]);
} else
printf("Input file was not found.");
}
In C array indices start at 0, so an array of size N has valid indices from 0 to N-1. In the code above you are accessing the array group out of bounds, since it has size 2 (valid indices are therefore 0 and 1), yet you are trying to access indices 1 and 2.
Either change:
char group[sum][2];
to:
char group[sum][3];
or use indices 0/1 rather than 1/2.
Note also that your code lacks error checking, e.g. on the call to fopen.
Your program as several issues:
you use 1 based indexing, which causes confusion and leads to referencing arrays and subarrays beyond their defined ends.
you parse the input with fscanf using the %s specifier: this is unsafe and will write 2 bytes for each of your inputs, writing beyond the end of each subarray and beyond the end of the last array.
You already know how to fix these, preferably by using 0 based indexing
Your algorithm is very ineffective, complexity O(n!) because you enumerate all possible permutations and check for validity only on complete permutations. You can drastically improve the performance by only enumerating permutations which already verify the constraint for their initial elements. The complexity is substantially lower, still quadratic but n is quite small.
Here is a modified version of your code that does this:
#include <stdio.h>
int permutation(char group[][2], int buffer, int sum) {
if (buffer == sum)
return group[sum-1][1] == group[0][0];
for (int i = buffer; i < sum; i++) {
if (group[buffer-1][1] == group[i][0]) {
char temp = group[buffer][0];
group[buffer][0] = group[i][0];
group[i][0] = temp;
temp = group[buffer][1];
group[buffer][1] = group[i][1];
group[i][1] = temp;
if (permutation(group, buffer + 1, sum))
return 1;
temp = group[buffer][0];
group[buffer][0] = group[i][0];
group[i][0] = temp;
temp = group[buffer][1];
group[buffer][1] = group[i][1];
group[i][1] = temp;
}
}
return 0;
}
int main(void) {
FILE *input = fopen("input.txt", "r");
int sum, i;
if (input != NULL) {
if (fscanf(input, "%d", &sum) != 1 || sum <= 0) {
printf("invalid number of pairs\n");
fclose(input);
return 1;
}
char group[sum][2];
for (i = 0; i < sum; i++) {
if (fscanf(input, " %c %c", &group[i][0], &group[i][1]) != 2) {
printf("incorrect input for pair number %d\n", i);
fclose(input);
return 1;
}
}
fclose(input);
if (permutation(group, 1, sum)) {
FILE *output = fopen("output.txt", "a");
if (output == NULL) {
printf("cannot open output file\n");
return 2;
}
for (i = 0; i < sum; i++) {
fprintf(output, "%c %c\n", group[i][0], group[i][1]);
}
fclose(output);
return 0;
} else {
printf("complete path not found\n");
return 1;
}
}
printf("cannot open input file\n");
return 2;
}
I modified other aspects of the code to improve efficiency and reusability:
input is checked for validity.
the recursive function stops and returns 1 when it finds a complete path. This allows the program to continue whether it found the path or not.
output is handled from the main function for consistency.
The above code solves the problem for the specified input with n=50 in less than 0.002 seconds on my laptop. It prints F C C E E F F E E E E E E E E E E B B F F E E A A F F C C A A A A E E F F C C E E E E E E E E E E B B C C E E E E F F E E F F F F E E C C E E E E E E B B F F A A D D A A C C C C E E E E E E B B D D F
EDIT I realized that, since you are looking for a full closed path, you do not need to try different possibilities for the first pair. main can call permutation with 1 instead of 0 and the permutation can be simplified as buffer can never be 0.
Your new code has some problems:
find is defined asa returning int, but you return nothing. You indeed do not test if you have found a complete path, fully relying on the assumption that there is at least one and that you have found it.
You do not test for path closure. You may find a closed path by chance, but you may also produce an unclosed path.
Using 2 loops to find the unused pairs is less efficient than using a temporary array used[sum].
The first pair is always the first, so you can simplify the find function a little.
Here is an improved version:
#include <stdio.h>
int find(char group[][2], int buffer, int sum, int path[], unsigned char used[]) {
int i;
char last = group[path[buffer-1]][1];
if (buffer == sum)
return last == group[0][0];
for (i = 1; i < sum; i++) {
if (!used[i] && last == group[i][0]) {
path[buffer] = i;
used[i] = 1;
if (find(group, buffer + 1, sum, path, used))
return 1;
used[i] = 0;
}
}
return 0;
}
int main() {
FILE *input = fopen("input.txt", "r");
if (input != NULL) {
int sum = 0, i;
fscanf(input, "%d", &sum);
char group[sum][2];
int path[sum];
unsigned char used[sum];
for (i = 0; i < sum; i++)
fscanf(input, " %c %c", &group[i][0], &group[i][1]);
path[0] = 0; // always start at first element
used[0] = 1;
for (i = 1; i < sum; i++)
used[i] = 0;
if (find(group, 1, sum, path, used)) {
FILE *output = fopen("output.txt", "a");
for (i = 0; i < sum; i++)
fprintf(output, "%c %c\n", group[path[i]][0], group[path[i]][1]);
}
} else {
printf("Input file was not found.");
}
return 0;
}
EDIT: I tested this new version with your large input file: it crashes on my laptop. The previous version with the permutation functions works like a charm, producing the complete path in 0.060 seconds. So there is a complete path and something is wrong with this find function.
There are few differences between the algorithms:
permutation uses less stack space: a single automatic array of size n*2 (200k) versus 3 automatic arrays total size n*(sizeof(int) + 3) (700k).
permutation uses fewer variables, so recursion uses less stack space, but both probably use more than 1 MB of stack space to recurse 100000 times.
find does more scans, where permutation swaps group pairs and always snaps the next one directly.
I reimplemented find without recursion and finally got it to produce a complete path. It is a different one and it takes much longer to compute, 3.5 seconds.
For larger input files, you definitely should not use recursion and you should even allocate the arrays from the heap with malloc.
Here is the non recursive code, using heap memory:
#include <stdio.h>
#include <stdlib.h>
int find(const char group[][2], int sum, int path[]) {
path[0] = 0;
if (sum <= 1)
return group[0][1] == group[0][0];
unsigned char *used = calloc((size_t)sum, sizeof(*used));
for (int buffer = 1, i = 1;; i++) {
if (i == sum) {
--buffer;
if (buffer == 0) {
free(used);
return 0;
}
i = path[buffer];
used[i] = 0;
} else
if (!used[i] && group[path[buffer-1]][1] == group[i][0]) {
path[buffer] = i;
if (buffer == sum - 1) {
if (group[i][1] == group[0][0]) {
free(used);
return 1;
}
} else {
buffer++;
used[i] = 1;
i = 0;
}
}
}
}
int main() {
FILE *input = fopen("input.txt", "r");
if (input != NULL) {
int sum = 0, i;
fscanf(input, "%d", &sum);
char (*group)[2] = calloc((size_t)sum, sizeof(*group));
int *path = calloc((size_t)sum, sizeof(*path));
for (i = 0; i < sum; i++)
fscanf(input, " %c %c", &group[i][0], &group[i][1]);
if (find(group, sum, path)) {
FILE *output = fopen("output.txt", "a");
for (i = 0; i < sum; i++)
fprintf(output, "%c %c\n", group[path[i]][0], group[path[i]][1]);
}
} else {
printf("Input file was not found.");
}
return 0;
}

printing substrings in a file

So I want to write a program which would print out a text line that contains a certain word from a file. e.g. if I was looking for a word 'linux' it would print out
2 computers called linux00, linux01 and linux02. 5 manager,"
said linux00. "Hello linux00," said 7 here to see us?" said
linux01. "Well," said the 10 linux02. "You're all going to be
unplugged," said 12 goooooooooooo..." said linux00.
from a story.txt:
Once upon a time, there were three little computers called
linux00, linux01 and linux02. One day, the nice computer manager
came into the Linux Laboratory. "Hello nice computer manager,"
said linux00. "Hello linux00," said the nice computer manager.
"What brings you here to see us?" said linux01. "Well," said the
nice computer manager, "I've got bad news and I've got good
news." "What's the bad news?" said linux02. "You're all going to be
unplugged," said the nice computer manager. "What's the
goooooooooooo..." said linux00.
Here's my code:
#include<stdio.h>
#include <stdlib.h>
#define ARR_LEN 100
int getLine(FILE * fin,char a[],int n)
{
int find = contains("linux", 5, a, ARR_LEN);
int count;
int i;
i = 0;
char c = getc(fin);
while(c != '\n')
{
a[i] = c;
// printf ("%c", a[i]);
//i = 0;
if (a[i] == EOF){
return EOF;
}
if (find == 1)
{
printf("%c", c);
c = getc(fin);
}
i = i + 1;
}
if(a[i]=='\n')
{
if ((i - 1) > ARR_LEN) {
printf("warning msg: length is over array bounds\n");
}
// printf("length of line is: %d\n", i - 1);
//printf("%c", a[i]);
i = i + 1;
//printf("\n");
return i - 1;
}
}
int contains(char target[], int m, char source[], int n) {
int flag = 0; // the source originally does not contain the target
int i;
for(i = 0; i < n; i++) { // go through each character of the source string
int targetIndex = 0;
int j;
/*check if the preceding characters of the source string are a substring
that matches the target string*/
for(j = i; j < n && targetIndex < m; j++) {
if(target[targetIndex] == source[j]) {
targetIndex += 1;
if(targetIndex == m) { // the 'target' has been fully found
flag = 1;
break;
}
}
else
{
break;
}
}
if(flag == 1) // 'target' is already found, no need to search further
{
break;
}
}
return flag;
}
main(int argc,char ** argv)
{
setbuf(stdout,NULL);
char a[ARR_LEN];
FILE * fin;
if(argc<2){
printf("wrong number of arguments\n");
exit(0);
}
fin = fopen(argv[1], "r");
if (fin == NULL) {
printf("Cannot open %s\n", fin);
exit(0);
}
int t = 0;
int j = 0;
int find = contains("linux", 5, a, ARR_LEN);
while (j != EOF)
{
t = t + 1;
printf("%d ", t);
j = getLine(fin,a,ARR_LEN);
printf("\n");
}
fclose(fin);
}
The getLine function is alright and it prints out a text with a line number in front all good. But the problem is with this
if (find == 1)
{
printf("%c", c);
c = getc(fin);
}
part, where I want the program to only print out the line if "contains" finds a match in that line.
Thanks for any help & sorry for a long post!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char sentence[500];
char word[10] = "linux";
FILE* fp1 = fopen("strstr.txt","r");
if(fp1 == NULL)
{
printf("Failed to open file\n");
return 1;
}
while((fscanf(fp1,"%[^\n]\n",sentence)>0))
{
if(strstr(sentence,word)!=NULL)
printf("%s\n\n",sentence);
}
}

I read a string from a file and store it into an array, but how do I know the length of the array?

#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;
}

Resources