Why this code doesn't update file? - c

this program create a file,write on the file, after read from that file, update data and then put the file's content into an array. When I run the code, it doesn't work right, in fact, all values printed except for Vet[i].squadra are zero. I debugged the code and I saw that AggiornaFile function resets to zero variable's values each time I use fseek and freed, so I think that fwrite update values into file with zeros.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define DIMMAX 4
#define DIMCAR 20
typedef char Stringa[DIMCAR];
typedef struct{
int vinte;
int perse;
int nulle;
} Partite;
typedef struct{
int subiti;
int effettuati;
} Goal;
typedef struct{
int squadra;
Partite partite;
Goal goal;
int punti;
} Nota;
typedef struct{
int squadra1;
int squadra2;
int goal1;
int goal2;
} Match;
void CreaFile(Stringa, int);
void DisputaMatch(Match *, int, int);
void AggiornaFile(Stringa, Match);
int CalcolaEsito(Match);
void DisputaFase(int, Stringa);
Nota CalcolaClassifica(Nota[], int, Stringa);
void ordina(Nota *, int);
void StampaClassifica(Nota[], int);
int calcolaNPartite(Nota);
void CreaFile(Stringa percorso, int valDati){
FILE *ptr;
Nota n;
int i;
ptr = fopen(percorso, "wb");
if(ptr ==NULL){
printf("File inesistente.\n");
}
else{
for(i=1; i<= valDati; i++){
n.partite.vinte = 0;
n.partite.perse = 0;
n.partite.nulle = 0;
n.goal.subiti = 0;
n.goal.effettuati = 0;
n.punti = 0;
n.squadra = i;
fwrite(&n, sizeof(Nota), 1, ptr);
}
fclose(ptr);
}
}
void DisputaMatch(Match *incontro, int S1, int S2){
int rand1,rand2;
incontro->squadra1 = S1;
incontro->squadra2 = S2;
rand1 = rand() % 5;
rand2 = rand() % 5;
incontro->goal1 = rand1;
incontro->goal2 = rand2;
}
void AggiornaFile(Stringa percorso, Match incontro){
FILE *ptr;
Nota n;
int esito;
ptr = fopen(percorso, "rb+");
if(ptr == NULL){
printf("File inesistente.\n");
}
else{
fseek(ptr, incontro.squadra1*sizeof(Nota), SEEK_SET);
fread(&n, sizeof(Nota), 1, ptr);
n.goal.effettuati += incontro.goal1;
n.goal.subiti += incontro.goal2;
esito = CalcolaEsito(incontro);
switch(esito){
case 0:
n.partite.nulle++;
n.punti += 1;
break;
case 1:
n.partite.vinte++;
n.punti += 3;
break;
case 2:
n.partite.perse++;
break;
}
fwrite(&n, sizeof(Nota),1, ptr);
fseek(ptr, incontro.squadra2*sizeof(Nota), SEEK_SET);
fread(&n, sizeof(Nota), 1, ptr);
n.goal.effettuati += incontro.goal2;
n.goal.subiti += incontro.goal1;
switch(esito){
case 0:
n.partite.nulle++;
n.punti += 1;
break;
case 1:
n.partite.perse++;
break;
case 2:
n.partite.vinte++;
n.punti += 3;
break;
}
fwrite(&n, sizeof(Nota),1, ptr);
fclose(ptr);
}
}
int CalcolaEsito(Match incontro){
int esito;
if(incontro.goal1 == incontro.goal2){
esito = 0;
}
else if(incontro.goal1 > incontro.goal2){
esito = 1;
}
else{
esito = 2;
}
return esito;
}
void DisputaFase(int valDati, Stringa percorso){
Match incontro;
int i,j;
for(i=1; i<=valDati; i++){
for(j=1; j<=valDati; j++){
if( j!= i){
DisputaMatch(&incontro, i, j);
AggiornaFile(percorso, incontro);
}
}
}
}
Nota CalcolaClassifica(Nota Vet[], int dim, Stringa percorso){
FILE *ptr;
Nota n;
int i = 0;
ptr = fopen(percorso, "rb");
if(ptr == NULL){
printf("File inesistente.\n");
}
else{
while(!feof(ptr)){
fread(&n, sizeof(Nota),1,ptr);
Vet[i] = n;
i++;
}
fclose(ptr);
}
ordina(Vet, dim);
return Vet[0];
}
void ordina(Nota *Vet, int dim){
int i,j;
int min;
Nota temp;
for(i=0; i<dim-1; i++){
min = i;
for(j=i+1; j<dim; j++){
if(Vet[min].punti < Vet[j].punti){
min = j;
}
}
if(min != i){
temp = Vet[min];
Vet[min] = Vet[i];
Vet[i] =temp;
}
}
}
void StampaClassifica(Nota Vet[], int dim){
int i,j;
printf("\n%s%15s%15s%18s%15s%15s%15s%15s\n", "Squadra", "Punteggio", " PartiteGiocate", "Vinte", "Perse", "Nulle", "Gsubiti", "Gfatti");
for(i=0; i<dim; i++){
printf("%7d%15d%15d%18d%15d%15d%15d%15d\n", Vet[i].squadra, Vet[i].punti, calcolaNPartite(Vet[i]), Vet[i].partite.vinte, Vet[i].partite.perse, Vet[i].partite.nulle,Vet[i].goal.subiti, Vet[i].goal.effettuati);
}
}
int calcolaNPartite(Nota nota){
int giocate;
giocate = nota.partite.nulle + nota.partite.perse + nota.partite.vinte;
return giocate;
}
int main(int argc, char *argv[]) {
FILE *ptr;
char percorso[300] = "C:\\file.dat";
Nota Vet[DIMMAX];
srand(time(NULL));
CreaFile(percorso, DIMMAX);
DisputaFase(DIMMAX, percorso);
CalcolaClassifica(Vet, DIMMAX, percorso);
StampaClassifica(Vet, DIMMAX);
return 0;
}

Note that fread and fwrite both advance the file pointer.
When wanting to read/modify/write in AggiornaFile you must fseek again before the write.
Also, be sure to use 0-based record indexing.

Related

data is not able to read in the c program

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
typedef struct arr_col_1{
int x;
};
typedef struct arr_col_2{
int x, y;
};
typedef struct array{
int x1, y1;
};
void col_1(FILE *file, int row);
void col_2(FILE *file, int row);
int main(){
FILE* file;
int row = 0, col = 1, i;
char fname[40], chr;
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
do{
printf("\nEnter the file which you want to vizualize = ");
scanf("%s", &fname);
file = fopen(fname, "r");
if(file == NULL){
printf("\nPlease Enter correct file name ");
}
}while(file == NULL);
chr = getc(file);
while(chr != EOF){
if(chr == '\n'){
row++;
}
if((row == 0)&&(chr == ',')){
col++;
}
chr = getc(file);
}
printf("%d row %d col", row, col);
if(col == 1){
col_1(file, row);
}
if(col == 2){
col_2(file, row);
}
getch();
return 0;
}
void col_1(FILE *file, int row){
struct arr_col_1 *arr1;
int i;
arr1 = (struct arr_col_1*)malloc(row * sizeof(struct arr_col_1));
for(i = 0; i < row; i++){
fscanf(file, "%d", &arr1[i].x);
printf("%d\n", arr1[i].x);
}
getch();
}
void col_2(FILE *file, int row){
struct arr_col_2 *arr2;
int i;
arr2 = (struct arr_col_2*)malloc(row * sizeof(struct arr_col_2));
for(i = 0; i < row; i++){
fscanf(file, "%d %d", &arr2[i].x, &arr2[i].y);
printf("%d %d\n", arr2[i].x, arr2[i].y);
}
getch();
}
the above code able to tell no of rows and col but it not show the data it is not able to read the data. please help me out in the above code i send file to the function but it look like i am not able to read the data. i dont know where the problem occurs please help. somewhere i think the problem lies between file pointer but code is run with no error

C - char** not printing correctly after being returned from function

I am trying to return a char** from a function to main so I can do stuff with it, but when I print it out it gives me random characters. I'm at a loss.
main VV
// ./main < data/filelist.txt
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hist.h"
int main(){
int count = 0;
int i;
char** files = read(&count);
displayList(files, count);
char** array = splitFiles(files, count);
printf("IN MAIN: array[0] - %s\n", array[0]);
Histogram* p;
printf("here\n");
int histArrayCount = calcHistogram(&array[0], &count, &p);
printf("here\n");
displayHistogram(p, histArrayCount);
printf("here\n");
}
hist.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hist.h"
int calcHistogram (char** a, int* count, Histogram** q) {
int i, k;
int j = 0;
int histArrayCount = 0;
Histogram* p = (Histogram*)malloc((*count) * sizeof(Histogram));
for (k = 0; k < *count; k++) {
p[k].num = "1";
p[k].freq = 0;
}
for (i = 0; i <= *count; i++) {
while ((strcmp(p[j].num, "1") != 0) && (strcmp(p[j].num, a[i]) != 0)) {
j++;
}
if (strcmp(p[j].num, "1") == 0) {
p[j].num = a[i];
p[j].freq = 1;
histArrayCount++;
}
else if ((strcmp(p[j].num, a[i]) == 0)) {
p[j].freq += 1;
}
else {
printf("ERROR\n");
}
j = 0;
}
*q = p;
return histArrayCount;
}
void displayHistogram (Histogram* p, int histArrayCount) {
int i;
for (i = 0; i < histArrayCount - 1; i++) {
printf("value %s: freq %d\n", p[i].num, p[i].freq);
}
printf("\n");
}
char** splitFiles(char** files, int count) {
int i;
char** array = (char**)malloc((count)*sizeof(char*));
FILE* fp;
int c = 0;
for(i = 0; i < count; i++) {
char buff[255];
fp = fopen(files[i], "r");
array[i] = fgets(buff, 255, (FILE*)fp);
printf("%d : %s\n", i, buff);
}
printf("array[0] = %s\n", array[0]);
return array;
}
void displayList(char** a, int c) {
int i;
for (i = 0; i < c; i++) {
printf("File %d: %s\n", i, a[i]);
}
printf("\n");
}
char** read(int* c){
int count = 0;
int i;
char** a = (char**)malloc(100*sizeof(char*));
for (i = 0; i< 100; i++) {
a[count] = (char*)malloc(100*sizeof(char));
count++;
}
count = 0;
int endOfFile = scanf("%s", a[count]);
while (endOfFile != EOF) {
count++;
endOfFile = scanf("%s", a[count]);
}
*c = count;
return a;
}
When I print the array at index 0 in here it gives me what is actually there, but when I do the same thing in main it does not. It gives me three random characters.
hist.h
typedef struct Histogram {
char* num;
int freq;
} Histogram;
char** read(int* c);
void displayList(char** a, int c);
int calcHistogram (char** a, int* c, Histogram** p);
void displayHistogram (Histogram* a, int histArrayCount);
char** splitFiles (char** files, int count);

C: Segmentation fault on function pointer

I am getting a segmentation fault error when trying to sort the structure by using pointer. I guess it makes problem in 'scanf_s' function in 'main()' since "Debug MSG 2" is not printed when it is executed. here is the full code.
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct contestant
{
char *name;
float height;
int weight;
} ppl;
typedef int(*Funcptr)(ppl *, ppl *);
int namecmp(ppl *, ppl *);
int heightcmp(ppl *, ppl *);
int weightcmp(ppl *, ppl *);
void sort(Funcptr, Funcptr, Funcptr, ppl *, int);
int main()
{
int i;
int num;
ppl *people;
scanf_s("%d", &num);
people = (ppl *)malloc(num * sizeof(ppl));
printf("Debug MSG 1\n");
for (i = 0; i<num; i++)
scanf_s("%s %f %d", people[i].name, &(people[i].height), &(people[i].weight));
printf("Debug MSG 2\n");
sort(namecmp, heightcmp, weightcmp, people, num);
sort(heightcmp, weightcmp, namecmp, people, num);
sort(weightcmp, namecmp, heightcmp, people, num);
free(people);
}
int namecmp(ppl *human1, ppl *human2)
{
char *temp;
if (strcmp(human1->name, human2->name) > 0)
{
temp = human1->name;
human1->name = human2->name;
human1->name = temp;
return 1;
}
else if (strcmp(human1->name, human2->name) == 0)
return 0;
else
return -1;
}
int heightcmp(ppl *human1, ppl *human2)
{
float temp;
if (human1->height > human2->height)
{
temp = human1->height;
human1->height = human2->height;
human2->height = temp;
return 1;
}
else if (human1->height == human2->height)
return 0;
else
return -1;
}
int weightcmp(ppl *human1, ppl *human2)
{
int temp;
if (human1->weight > human2->weight)
{
temp = human1->weight;
human1->weight = human2->weight;
human2->weight = temp;
return 1;
}
else if (human1->weight > human2->weight)
return 0;
else
return -1;
}
void sort(Funcptr func1, Funcptr func2, Funcptr func3, ppl *person, int number)
{
int i, j;
int res1, res2;
for (i = 0; i<number - 1; i++)
{
for (j = i + 1; j<number; j++)
{
res1 = func1((person + i), (person + j));
if (res1 == 0)
{
res2 = func2((person + i), (person + j));
if (res2 == 0)
{
func3((person + i), (person + j));
}
}
}
}
for (i = 0; i<number; i++)
{
printf("%s %.1f %d\n", (person + i)->name, (person + i)->height, (person + i)->weight);
}
}
you're mallocing the table of people all right
people = (ppl *)malloc(num * sizeof(ppl));
(except that you don't have to cast return of malloc, but that's a detail)
but that doesn't dispense you to allocate memory for your name member structure
for (i = 0; i<num; i++)
scanf_s("%s %f %d", people[i].name, ...
Also as BLUEPIXY noticed, you're not using scanf_s properly (I thought it was scanf), you need to pass the max numbers of chars or simply use scanf with a size limitation.
fix it like that for instance:
for (i = 0; i<num; i++)
{
people[i].name = malloc(51);
scanf("%50s %f %d", people[i].name, ....
}
Alternate solution: define your structure as follows:
typedef struct contestant
{
char name[50];
float height;
int weight;
} ppl;
so no need to malloc the name. Allocating the array of ppl like you did is enough.

How do you copy a Struct array to another struct in the same array?

I am trying to work through my code, I'm unable to use a sorting algorithm due to crashes caused by trying to copy a structure at a specific array position to another in the same array at a different location.
//include statements
#include <stdio.h>
#include <stdlib.h>
//defines
#define MAX 100
#define STRUCTMAX 26
#define STUDENTS 9
//Struct declarations
typedef struct{
char street[STRUCTMAX];
char city[STRUCTMAX];
char state[STRUCTMAX];
char zip[STRUCTMAX];
}Address;
typedef struct{
char firstName[STRUCTMAX];
char initial[STRUCTMAX];
char lastName[STRUCTMAX];
Address adress;
int age;
double gpa;
}Student;
//prototypes
void readFile(Student students[]);
void printAll(Student students[]);
void printBestGpaName(Student students[]);
double averageGPA(Student students[]);
void printAboveAverageGPA(Student students[]);
void printYoungestLowGPA(Student students[]);
void sortStruct(Student students[]);
void strSub(char s1 [], char s2 [], int start, int size);
void initialize(Student students[]);
void main(void){
Student students[STUDENTS];
readFile(students);
printAll(students);
printBestGpaName(students);
printf("Average G.P.A is %.2lf\n" ,averageGPA(students));
printAboveAverageGPA(students);
printYoungestLowGPA(students);
sortStruct(students);
printf("\n");
printAll(students);
}
void readFile(Student students[]){
int i = 0;
char numberValue[10];
char line[MAX];
FILE *fp;
fp = fopen(
"/Users/derekroy/Desktop/Lab_6/Lab_6A/Students.dat", "r");
if (fp == NULL) {
printf("Students.dat file not found.\n");
exit(1);
}
while (!feof(fp)) {
fgets(line, MAX, fp);
strSub(line, students[i].firstName, 0, 7);
strSub(line, students[i].initial, 10, 1);
strSub(line, students[i].lastName, 11, 9);
strSub(line, students[i].adress.street, 20, 16);
strSub(line, students[i].adress.city, 36, 13);
strSub(line, students[i].adress.state, 49, 2);
strSub(line, students[i].adress.zip, 52, 5);
strSub(line, numberValue, 58, 2);
students[i].age = atoi(numberValue);
strSub(line, numberValue, 60, 5);
students[i].gpa = atof(numberValue);
i++;
}
}
void printAll(Student students[]){
int i;
printf("All listed Students: \n");
for(i = 0; i < STUDENTS; ++i){
printf("%s %s %s %s %s %s, %s %d %.2lf\n" , students[i].firstName, students[i].initial,
students[i].lastName, students[i].adress.street, students[i].adress.city,
students[i].adress.state, students[i].adress.zip, students[i].age, students[i].gpa);
}
printf("\n");
printf("******");
}
void printBestGpaName(Student students[]){
int i, best = 0;
for(i = 0; i < STUDENTS; ++i){
if(students[i].gpa > students[best].gpa)
best = i;
}
printf("Student with best G.P.A: ");
printf("%s %s %s\n" , students[best].firstName, students[best].initial, students[best].lastName);
}
double averageGPA(Student students[]){
int i;
double sum = 0.0;
for(i = 0; i < STUDENTS; ++i){
sum += students[i].gpa;
}
return sum / i;
}
void printAboveAverageGPA(Student students[]){
int i;
double average = averageGPA(students);
printf("Students with above average G.P.A: \n");
for(i = 0; i < STUDENTS; ++i){
if(students[i].gpa > average)
printf("%s %s %s\n" , students[i].firstName, students[i].initial, students[i].lastName);
}
}
void printYoungestLowGPA(Student students[]){
int i, j, young = 1000;
double average = averageGPA(students);
for(i = 0; i < STUDENTS; ++i){
if(students[i].gpa < average){
if(students[i].age < young){
j = i;
young = students[i].age;
}
}
}
printf("The youngest student with a below average G.P.A: ");
printf("%s %s %s\n" ,students[j].firstName, students[j].initial, students[j].lastName);
}
void sortStruct(Student students[]){
int i, j;
Student temp;
for(i = 1; i < STRUCTMAX; ++i){
/*temp.firstName = students[i].firstName;
temp.initial = students[i].initial;
temp.lastName = students[i].lastName;
temp.adress.street = students[i].adress.street;
temp.adress.city = students[i].adress.city;
temp.adress.state = students[i].adress.state;
temp.adress.zip = students[i].adress.zip;
temp.age = students[i].age;
temp.gpa = students[i].gpa;*/
temp = students[i];
j = i - 1;
while(j >= 0 && temp.gpa < students[j].gpa){
//students[j+1] = students[j];
j = j - 1;
}
//students[j+1] = temp;
}
}
void strSub(char s1 [], char s2 [], int start, int size){
int i;
for(i = 0; i < size; ++i){
s2[i] = s1[start];
start++;
}
s2[i] = '\0';
}
The offending line is in the Sort function.
How can I make this work, and copy the specified structure to their new address in the structure array?
you defined only 9 student records
Student students[STUDENTS]; -> STUDENTS macro is 9
you are iterating till STRUCTMAX which is a macro defined as 26
so you will go out of bounds causing the crash
for(i = 1; i < STRUCTMAX; ++i){

Invalid pointer with pthread_join

#include <pthread.h> /* Thread related functions*/
#include <stdio.h> /* Standard buffered input/output */
#include <stdlib.h> /* Standard library functions */
#include <string.h> /* String operations */
struct element{
FILE* file1;
FILE* file2;
int alone;
};
int comp( const void* a, const void* b) //function for qsort
{
const int *a1 = (const int *)a; // casting pointer types
const int *b1 = (const int *)b;
return *a1 - *b1;
}
void* merge(void* filenames) //writes out files to two arrays, merges them and writes output to temp file and passes this tempfile through pthread_exit.
{
struct element* Data;
Data = (struct element*) filenames;
//char* fileA = Data->file1;
//char* fileB = Data->file2;
FILE* A = Data->file1;
FILE* B = Data->file2;
if(Data->alone!=1)
{
//A = fopen(fileA, "r");
int linesA = 0;
int val=0;
printf("FILE* A: %p \n", A);
rewind(A);
while(fscanf(A, "%d", &val) != EOF)
linesA++;
printf("scanf\n");
rewind(A);
int* intarrA = (int*) malloc(linesA*sizeof(int));
int i =0;
for(;fscanf(A, "%d", &val) != EOF; i++)
intarrA[i] = val;
fclose(A);
//FILE* B;
//B = fopen(fileB, "r");
int linesB = 0;
while(fscanf(B, "%d", &val) != EOF)
linesB++;
rewind(B);
int* intarrB = (int*) malloc(linesB*sizeof(int));
i =0;
for(;fscanf(B, "%d", &val) != EOF; i++)
intarrB[i] = val;
fclose(B);
int templength = linesA+linesB;
int* inttemp = (int*) malloc(templength*sizeof(int));
int help1 = 0;
int help2 = 0;
int temph = 0;
for(i=0; i<templength; i++)
{
if(help1 == linesA)
{
int j = 0;
for(j=help2; j<linesB; j++)
{
inttemp[temph] = intarrB[j];
temph++;
}
break;
}
else if(help2 == linesB)
{
int j = 0;
for(j=help1; j<linesA; j++)
{
inttemp[temph] = intarrA[j];
temph++;
}
break;
}
else if(intarrA[help1]==intarrB[help2])
{
inttemp[temph]=intarrA[help1];
help1++;
help2++;
temph++;
}
else if (intarrA[help1]<intarrB[help2])
{
inttemp[temph]=intarrA[help1];
help1++;
temph++;
}
else if(intarrA[help1]>intarrB[help2])
{
inttemp[temph]=intarrB[help2];
help2++;
temph++;
}
}
FILE* filetowrite;
filetowrite = tmpfile();
for(i=0; i<temph; i++)
fprintf(filetowrite ,"%d\n",inttemp[i]);
printf("Merged %d lines and %d lines into %d lines\n",linesA,linesB,temph);
// free(intarrA);
// free(intarrB);
// free(inttemp);
printf("thread exit\n");
pthread_exit((void*)filetowrite);
}
else{
pthread_exit((void*)Data->file1);
printf("ELSE MERGE \n");
}
}
void* worker(void* filen)
{
//left out to keep code short
}
int main(int argc, char **argv) //gets files through terminal, and sorts each one trhough worker function and then merges them with merge function and pthreads.
{
int zit =0;
void* tempf;
// tempf = malloc(sizeof(FILE));
argc = argc-1;
struct element* filenamelist; //I make an array of this to merge threads till there is only one element left
pthread_t *threadid;
threadid = (pthread_t*) malloc(sizeof(pthread_t)*argc);
int i;
for(i=1; i<=argc; i++) // part 1 passing the files to be qsorted
{
pthread_create(&threadid[i-1], NULL, worker, (void*) argv[i]);
}
//sorts each file fine. saves it as filename.sorted
for(i=0; i<argc; i++)
{
pthread_join(threadid[i], NULL);
}
printf(" DONE WORKER\n");
int mall=0;
int size = 0;
if(size%2 ==0)
size = argc/2;
else
size = argc/2 +1;
//int Osize = size;
int z=0;
int truth = 0;
// filenamelist = (struct element**) malloc(sizeof(struct element*)*argc);
// for(i=0; i<argc; i++)
filenamelist = (struct element*) malloc(argc*(sizeof(struct element)));
FILE** inputFiles = malloc(sizeof(FILE*) * (argc+argc));
for(i=1; i<=argc; i++) //creates a list of elements with file ptrs
{
filenamelist[(i-1)/2].alone = 0;
if(i==argc && i%2!=0)
{
char* tedious1 = (char*) malloc( (strlen(argv[i]) + 8 ));
//char* tedious1 = (char*) malloc((strlen(argv[i+1]+7))*sizeof(char));
strcpy(tedious1,argv[i]);
filenamelist[(i-1)/2].file1 = fopen(strcat(tedious1,".sorted"),"r");
filenamelist[(i-1)/2].file2 = NULL;
filenamelist[(i-1)/2].alone = 1;
free(tedious1);
}
else
{
char* tedious3 = (char*)malloc( (strlen(argv[i]) + 8 ));
strcpy(tedious3,argv[i]);
// printf("%s\n", tedious3);
if(i%2 ==0)
filenamelist[i/2].file2 = fopen(strcat(tedious3,".sorted"),"r");
else
filenamelist[i/2].file1 = fopen(strcat(tedious3,".sorted"), "r");
free(tedious3);
}
}
/* for(i=0; i<size; i++)
{
printf("THIS IS: %d\n", i);
if(filenamelist[i].alone ==1)
{
printf(" Alone \n");
printf(" %p \n", filenamelist[i].file1);
}
else
{
printf("1 %p \n", filenamelist[i].file1);
printf("2 %p \n", filenamelist[i].file2);
}
}*/
// pthread_t* threadid2;
// threadid2 = (pthread_t*) malloc(sizeof(pthread_t)*(2*argc));
int xyz = 0;
int arab = 0;
int saudi = 0;
while(size>1) //Iterates through list till only one element left
{
i = 0;
pthread_t* threadid2;
threadid2 = (pthread_t*) malloc(sizeof(pthread_t)*argc);
printf("before create: %d, %p \n", i, tempf);
for ( ; i<size;i++ )
{
pthread_create(&threadid2[i], NULL, merge, &filenamelist[i]); //<--- invalid pointer
printf("AFTER create: %d, %p \n", i, threadid2[i]);
}
i = 0;
for ( ; i<size; i++)
{
printf("before join JOIN: %d, %p \n", i, tempf);
pthread_join(threadid2[i], &tempf);
printf("AFTER JOIN: %d, %p \n", i, tempf);
if (i%2 == 0)
{
filenamelist[i/2].file1 = (FILE*)(tempf);
}
else
{
filenamelist[i/2].file2 = (FILE*)(tempf);
}
if(i%2==0 && i ==size-1)
filenamelist[i].alone = 1;
zit=0;
truth = 0;
while(zit<z)
{
if(inputFiles[zit] == (FILE*)(tempf))
truth = 1;
zit++;
}
if(truth != 1)
{
inputFiles[z] = (FILE*)(tempf);
z++;
}
}
if(size==1)
size = 0;
else if (size % 2 == 0)
size = size/2;
else
size = (size/2)+1;
free(threadid2);
}
pthread_t throwthread;
pthread_create(&throwthread, NULL, merge, &filenamelist[0]);
FILE* final;
pthread_join(throwthread, tempf);
int chd = 0;
final = (FILE*) tempf;
// if(0!=feof(tempf))
// rewind(tempf);
//rewind(filenamelist[0]->file1);
int finish = 0;
//printf("file 1:%p",tempf);
while(fscanf(final, "%d", &chd) != EOF)
finish++;
rewind(final);
int* finarr = (int*) malloc(finish*sizeof(int));
int xx =0;
for(;fscanf(final, "%d", &chd) != EOF; xx++)
finarr[xx] = chd;
//fclose(filetosort);
//qsort(intarr, i, sizeof(int),comp);
FILE* filetowrites;
filetowrites = fopen("sorted.txt", "w");
for(xx=0; xx<finish; xx++)
fprintf(filetowrites, "%d\n", finarr[xx]);
fclose(filetowrites);
for(xx=0; xx<z; xx++)
fclose(inputFiles[xx]);
free(inputFiles);
free(filenamelist);
free(finarr);
return 0;
}//end main func
This gives me an invalid pointer error. threadid is a pthread_t* pointing to an array.
I'm passing in a file pointer typecasted into a void* for the pthread_exit((void*)fileptr);
where fileptr is some FILE*.
And would it be okay to access tempf as (FILE*) tempf?
EDIT:I've tried everything that makes sense to me. Don't know why threadid2[i] or tempf would be an invalid pointer in the merge function. This is not something i can debug, Help please!
ERROR OCCURS ON FIRST JOIN

Resources