This is the code I am working on and in the function trace* readTrace(char* fileName) I have to read a file (that fills structure) and then return the address of trace structure. Also the time and value of the structure are pointers but I don't know how to do it.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#define TMAX 1000
#define NBPTS 2000
#define DT 0.5
typedef struct
{
char comment[40];
int nbpts;
float time[4096];
float value[4096];
} trace;
void simuTrace(int tmax, float dt, float params[], trace *uneTrace)
{
printf("Shkruani emrin e eksperimentit : \n");
scanf("%s", &uneTrace->comment);
int i = 0;
float v = 0, w = 0, dv = 0, dw = 0, t = 0;
float a = params[0], d = params[1], e = params[2];
while (t <= tmax)
{
dv = (a - v) * (v - 1) * v - w;
dw = e * (0.5 * v - w - d);
v += dv * dt;
w += dw * dt;
uneTrace->time[i] = t;
uneTrace->value[i] = v;
i++;
t += dt;
}
uneTrace->nbpts = i;
}
void printTrace(trace uneTrace)
{
printf("%s\n", uneTrace.comment);
printf("\t%d\n", uneTrace.nbpts);
int i;
for (i = 0; i <= NBPTS; i++) {
printf(" t= %.1f \tv= %.4f \n ", uneTrace.time[i],uneTrace.value[i]);
}
}
void saveTraceBin(char *fileTrace, trace uneTrace)
{
FILE *file;
file = fopen(fileTrace, "w");
if (fopen(fileTrace, "w") == NULL) {
printf("\n Gabim! \n");
} else {
fprintf(file, "%s\n", uneTrace.comment);
fprintf(file, "%d", uneTrace.nbpts);
int i;
for (i = 0; i <= NBPTS; i++) {
fprintf(file, "\n %.1f %.4f",uneTrace.time[i],uneTrace.value[i]);
}
fclose(file);
}
}
void readTrace(char *fileName, trace *uneTrace)
{
FILE*file;
file = fopen(fileName, "r");
if (file != NULL) {
fscanf(file, "%s", uneTrace->comment);
fscanf(file, "%d", &uneTrace->nbpts);
int i;
for (i = 0; i <= NBPTS; i++) {
fscanf(file, "%f", &(uneTrace->time[i]));
fscanf(file, "%f", &(uneTrace->value[i]));
}
printf("\n Leximi perfundoi me sukses!\n");
} else {
printf("\n Gabim! \n");
}
fclose(file);
}
trace* readTrace(char* fileName) {
FILE*file;
file = fopen(fileName, "r");
if (file != NULL) {
fscanf(file, "%s", uneTrace->comment);
fscanf(file, "%d", &uneTrace->nbpts);
int i;
for (i = 0; i <= NBPTS; i++) {
fscanf(file, "%f", &(uneTrace->time[i]));
fscanf(file, "%f", &(uneTrace->value[i]));
}
printf("\n Leximi perfundoi me sukses!\n");
} else {
printf("\n Gabim! \n");
}
fclose(file);
}
float errorTrace(trace uneTrace1, trace uneTrace2)
{
float sum = 0;
int i;
for (i = 0; i <= NBPTS; i++)
{
sum += (uneTrace1.value[i] - uneTrace2.value[i]);
}
sum /= NBPTS;
return sqrt(fabs(sum));
}
int main()
{
float Pa[] = { 0.5, 0.01, 0.05 };
float Pb[] = { 0.75, 0.3, 0.1 };
float e1, e2;
trace tracePa, tracePb, traceCell;
simuTrace(NBPTS, DT, Pa, &tracePa);
printTrace(tracePa);
saveTraceBin("myfile1.txt", tracePa);
simuTrace(NBPTS, DT, Pb, &tracePb);
printTrace(tracePb);
saveTraceBin("myfile2.txt", tracePb);
readTrace("cell.txt", &traceCell);
e1 = errorTrace(traceCell, tracePa);
e2 = errorTrace(traceCell, tracePb);
printf("\n Gabimi nga llogaritja e Pa : %f ", e1);
printf("\n Gabimi nga llogaritja e Pb : %f ", e2);
if (e1 < e2)
printf("\n\n Rasti Pa eshte me i mire se rasti Pb \n");
else
printf("\n\n Rasti Pb eshte me i mire se rasti Pa \n");
return 0;
}
You can either return a trace* that is allocated in readTrace,
trace* readTrace(char* fileName) {
trace *tp = malloc(sizeof *tp);
if (!tp) return NULL;
// fill up tp from file
....
}
// call this in main
trace *t = readTrace("cell.txt");
free(t); // free it when done
Or you could supply trace to the function, like
void readTrace(char* filename, trace *tp) {
if (!tp) return;
// fill up tp from file
....
}
// call this in main
trace t; // define trace object
readTrace("cell.txt", &t);
As for fill up time and value, read the array from the file:
int i;
fscanf(file, "%d", &t->nbpts);
for (i = 0; i < t->nbpts; i++) {
fscanf(file, "%f", &(t->time[i]));
fscanf(file, "%f", &(t->value[i]));
}
The function structure should look like this
trace* readTrace(char* fileName)
{
trace* traceptr;
int no_of_elements;
// Read the no of trace elements stored in file
// Generally this is avaliable in a location in the start of file,
// If not, then you have to guess and resize if it falls short.
traceptr = malloc(sizeof(trace) * no_of_elements);
// Read the trace elements from file
return (traceptr);
}
To call it in main()
int main(void)
{
trace *traceptr;
// Other stuff
traceptr = readTrace(filename);
simuTrace(NBPTS, DT, Pa, traceptr);
}
You need to modify your printTrace function to take a pointer to struct as input instead of an entire structure. Then it can take traceptr as it's input. Similarly for the saveTraceBin function.
Related
I try to add a new line to the already existing file, which already consists some text. I found out that when the file contains something, the program stops working after fputs(stringToAdd, myFile); in function void Save(struct SEED arrayToSave[], int size). However, when I try to write in an empty file, everything works properly. Can you guys please help me?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int numberOfItems = 0;
char path[] = "Catalogue.txt";
char text[1000000];
struct SEED {
int index;
char type[30];
char name[60];
float mass;
float price;
};
void GetText() {
FILE* catalogueFile;
char ch; // temporary char
int counter = 0;
catalogueFile = fopen(path, "r");
while((ch = fgetc(catalogueFile)) != EOF ) {
text[counter] = ch;
counter++;
}
fclose(catalogueFile);
}
void CalculateNumberOfItems() {
for(int i = 0; i < strlen(text); i++) {
if(text[i] == '\n') numberOfItems++;
}
}
struct SEED* GetArrayOfStructs() {
GetText();
CalculateNumberOfItems();
FILE* catalogueFile;
int counter = 0;
char* arrayOfSubstrings[numberOfItems];
struct SEED* arrayOfStructs = malloc(numberOfItems * sizeof(arrayOfStructs));
struct SEED* arrayOfStructsPointer = arrayOfStructs;
char* token = strtok(text, "\n");
while( token != NULL ) {
arrayOfSubstrings[counter] = token;
token = strtok(NULL, "\n");
counter++;
}
for(int i = 0; i < numberOfItems; i++) {
struct SEED tempStruct;
sscanf(arrayOfSubstrings[i], "%d %s %s %f %f", &tempStruct.index, tempStruct.type, tempStruct.name, &tempStruct.mass, &tempStruct.price);
arrayOfStructs[i] = tempStruct;
}
return arrayOfStructsPointer;
}
void Save(struct SEED arrayToSave[], int size) {
FILE* myFile = fopen("Catalogue.txt", "w");
char fullString[1000000];
for(int i = 0; i < size; i++) {
struct SEED currentStruct = arrayToSave[i];
char stringToAdd[1000];
sprintf(stringToAdd, "%d %s %s %f %f\n", currentStruct.index, currentStruct.type, currentStruct.name, currentStruct.mass, currentStruct.price);
strcat(fullString, stringToAdd);
}
printf("%s\n", fullString);
fputs(fullString, myFile);
fclose(myFile);
}
void AddNew() {
struct SEED* oldArrayOfStructs = GetArrayOfStructs(path);
struct SEED newArrayOfStructs[numberOfItems + 1];
struct SEED newStruct;
int newIndex = numberOfItems + 1;
char newType[30], newName[60];
float newMass, newPrice;
char tempChar[200];
printf("Input type, name, mass and price\nof the new seed whith spaces: ");
fgets(tempChar, 200, stdin);
fgets(tempChar, 200, stdin);
sscanf(tempChar, "%s %s %f %f\\n", newType, newName, &newMass, &newPrice);
newStruct.index = newIndex;
strcpy(newStruct.type, newType);
strcpy(newStruct.name, newName);
newStruct.mass = newMass;
newStruct.price = newPrice;
printf("%d\n", numberOfItems);
for(int i = 0; i < numberOfItems; i++) {
newArrayOfStructs[i] = oldArrayOfStructs[i];
}
newArrayOfStructs[numberOfItems] = newStruct;
Save(newArrayOfStructs, numberOfItems + 1);
printf("\n");
}
void UserInput() {
char choice;
int choiceInt, a = 1;
printf("Add new item - print 1\n");
printf("\n");
printf("Input your choice: ");
choice = getchar();
choiceInt = choice - '0';
printf("\n");
switch(choiceInt) {
case 1:
AddNew();
break;
default:
printf("Bye!\n");
break;
}
}
int main() {
UserInput();
return 0;
}
At least these problems:
Allocation too small
Code incorrectly allocates for an array of pointers.
// struct SEED* arrayOfStructs = malloc(numberOfItems * sizeof(arrayOfStructs));
struct SEED* arrayOfStructs = malloc(numberOfItems * sizeof arrayOfStructs[0]);
char too small to distinguish 257 different responses from fgetc()
// char ch; // temporary char
int ch;
...
while((ch = fgetc(catalogueFile)) != EOF ) {
Potential buffer overrun
Use a width.
No point scanning for 2 characters \ and n with "\\n".
// sscanf(tempChar, "%s %s %f %f\\n", newType, newName, &newMass, &newPrice);
sscanf(tempChar, "%29s %59s %f %f", newType, newName, &newMass, &newPrice);
Best to test sscanf() result too.
if (sscanf(tempChar, "%29s %59s %f %f", newType, newName, &newMass, &newPrice) != 4) {
TBD_error_code();
}
Overflow risk
// while((ch = fgetc(catalogueFile)) != EOF ) {
while(counter < (sizeof text - 1) && (ch = fgetc(catalogueFile)) != EOF ) {
text[counter] = ch;
Maybe overflow risk
// sprintf(stringToAdd, "%d %s %s %f %f\n",
snprintf(stringToAdd, sizeof stringToAdd, "%d %s %s %f %f\n",
I'm trying to read a .txt file. The task is to read temperatures and give out min, max and average values. The txt file is called Temp.txt and it looks like this :
5 76 56 34 35 10
4 45 23 24 14
0
2 32 34
Here is the code that I've written. I have also tried to run it using just the file name like 'fopen("Temp.txt","r")' but I get the same result.
#include<stdio.h>
#include<string.h>
int ReadTempFile(FILE *fp, float temperatur[]);
float maxTemp(int anzahl, float temperatur[]);
float minTemp(int anzahl, float temperatur[]);
float mittlereTemp(int anzahl, float temperatur[]);
float fahrenheit(float celsius);
int ReadTempFile(FILE *fp, float temperatur[]) //For reading individual rows from txt file
{
int i=0;
fscanf(fp,"%f", &temperatur[0]);
for(i = 0; i < temperatur[0]; ++i)
{
fscanf(fp, "%f", &temperatur[i+1]);
}
return temperatur[0];
}
float maxTemp(int anzahl, float temperatur[]) //Maximum Temperature
{
int i = 0;
float max;
max = temperatur[i+1];
for(i = 1; i < anzahl; i++)
{
if(temperatur[i+1] > max)
{
max = temperatur[i+1];
}
}
return max;
}
float minTemp(int anzahl, float temperatur[]) //Minimum Temperature
{
int i = 0;
float min;
min = temperatur[i+1];
for(i = 1; i < anzahl; i++)
{
if(temperatur[i+1] < min)
{
min = temperatur[i+1];
}
}
return min;
}
float mittlereTemp(int anzahl, float temperatur[]) //Average Temperature
{
int i, sum = 0;
float mit;
for (i = 0; i <= anzahl; i++)
{
sum += temperatur[i];
}
mit = sum/temperatur[0];
return mit;
}
float fahrenheit(float celsius) //Celsius to Fahrenheit
{
float f;
f = (celsius*9/5) + 32;
return f;
}
int main()
{
int end, n, Zeile=1;
float temperatur[20], max, min, mit, fmax, fmin, fmit;
char eingabe[20];
FILE *fp=NULL;
do
{
printf("Enter File name: \n"); //Enter file name
fflush(stdout);
scanf("%s", eingabe);
fp = fopen(eingabe, "r" );
if(fp == NULL) printf ("Error: File %s can't be opened!\n", eingabe); //Error message for File cant be opened
}while(fp != NULL);
do{
n = ReadTempFile(fp, temperatur);
max = maxTemp(n, temperatur);
printf("Das Maximum der Zeile %d ist: %.3fC \t",Zeile, max);
fmax = fahrenheit(max);
printf("In Fahrenheit: %.3fF\n", fmax);
min = minTemp(n, temperatur);
printf("Das Minimum der Zeile %d ist: %.3fC \t",Zeile, min);
fmin = fahrenheit(min);
printf("In Fahrenheit: %.3fF\n", fmin);
mit = mittlereTemp(n, temperatur);
printf("Der Mittelwert der Zeile %d ist: %.3fC \t",Zeile, mit);
fmit = fahrenheit(mit);
printf("In Fahrenheit: %.3fF\n", fmit);
++Zeile;
end = feof(fp);
printf("\n\n");
}while (end == 0);
fclose(fp);
return 0;
}
This is what happens after I run the above program.
Thank you in advance.
The most immediate problem that is likely causing the crash is the logic for opening a file:
do
{
printf("Enter File name: \n"); //Enter file name
fflush(stdout);
scanf("%s", eingabe);
fp = fopen(eingabe, "r" );
if(fp == NULL) printf ("Error: File %s can't be opened!\n", eingabe);
}while(fp != NULL); // <- Loops until it fails
This will guarantee that the loop will not end until a file fails to open. When you later try to fscanf from fp set to NULL, you'll have undefined behavior (and this may manifest itself as a crash).
Suggested fix:
for(;;) {
puts("Enter File name:");
if(scanf("%19s", eingabe) == 1) { // make sure scanf succeeds
fp = fopen(eingabe, "r" );
if(fp) break; // break out when it succeeds to open
perror(eingabe);
} else {
fprintf(stderr, "Error: No filename entered.\n");
return 1;
}
}
Note: In your example-run, you showed that you entered Temp to open Temp.txt. You must enter Temp.txt to successfully open Temp.txt.
Here is your program with some other fixes too. I've commented inline so you see why I suggest the changes.
Notes:
I've removed the +1 offset in temperatur completely. You shouldn't use the first float to store the count of temperatures which is an integer value.
Your mittlereTemp didn't use this offset, so it gave the wrong answers too.
mittlereTemp didn't calculate the median temperature (which I think "mittlereTemp" means). It tried to calculate the average temperature, so I changed the name to durchschnittsTemp.
#include <math.h>
#include <stdio.h>
#include <string.h>
// For reading individual rows from txt file
// max_anzahl added to not try to read too many
int ReadTempFile(FILE* fp, float temperatur[], int max_anzahl) {
int count;
// return -1 on failure
if(fscanf(fp, "%d", &count) != 1) return -1;
// we can't read all the entries if count > max_anzahl
if(count > max_anzahl || count < 0) return -1;
for(int i = 0; i < count; ++i) {
if(fscanf(fp, "%f", &temperatur[i]) != 1) {
return -1; // could not read count floats, failure
}
}
return count;
}
// The idiomatic way is to have the array pointer first and
// the count second so I swapped the order of temperatur and anzahl:
float maxTemp(float temperatur[], int anzahl) {
if(anzahl == 0) return NAN; // if we have no values, we have no max temp
float max = temperatur[0];
for(int i = 1; i < anzahl; i++) {
if(temperatur[i] > max) {
max = temperatur[i];
}
}
return max;
}
float minTemp(float temperatur[], int anzahl) {
if(anzahl == 0) return NAN; // if we have no values, we have no min temp
float min = temperatur[0];
for(int i = 1; i < anzahl; i++) {
if(temperatur[i] < min) {
min = temperatur[i];
}
}
return min;
}
// I changed the name to durchschnittsTemp
float durchschnittsTemp(float temperatur[], int anzahl) {
if(anzahl == 0) return NAN; // if we have no values, we have no average temp
float sum = 0.f; // use the proper type for sum
for(int i = 0; i < anzahl; i++) {
sum += temperatur[i];
}
return sum / (float)anzahl;
}
float fahrenheit(float celsius) {
float f;
f = (celsius * 9 / 5) + 32;
return f;
}
// since opening the file is a lot of code, make a function
FILE* open_file_supplied_by_the_user() {
char eingabe[FILENAME_MAX]; // make plenty of room for the filename
for(;;) {
puts("Enter File name:");
// use fgets instead of scanf to make it easier to read filenames with spaces
if(fgets(eingabe, sizeof eingabe, stdin)) {
size_t len = strlen(eingabe);
eingabe[len - 1] = '\0';
FILE* fp = fopen(eingabe, "r");
if(fp) return fp; // break out when it succeeds to open
perror(eingabe); // give the user a proper error message
} else {
fprintf(stderr, "Error: No filename entered.\n");
return NULL;
}
}
}
#define Size(x) (sizeof(x) / sizeof *(x))
int main() {
FILE* fp = open_file_supplied_by_the_user();
if(fp == NULL) return 1; // exit if no file was opened
float temperatur[20];
// loop until ReadTempFile() returns -1 (failure)
for(int n, Zeile = 1;
(n = ReadTempFile(fp, temperatur, Size(temperatur))) != -1;
++Zeile)
{
float max = maxTemp(temperatur, n);
printf("Das Maximum der Zeile %d ist: %.3fC \t", Zeile, max);
printf("In Fahrenheit: %.3fF\n", fahrenheit(max));
float min = minTemp(temperatur, n);
printf("Das Minimum der Zeile %d ist: %.3fC \t", Zeile, min);
printf("In Fahrenheit: %.3fF\n", fahrenheit(min));
float mit = durchschnittsTemp(temperatur, n);
printf("Der Durchschnittswert der Zeile %d ist: %.3fC \t", Zeile, mit);
printf("In Fahrenheit: %.3fF\n", fahrenheit(mit));
printf("\n\n");
}
fclose(fp);
}
I want to use pointer in here, but it's not right.
It has Thread 1: breakpoint 1.1 (1) in
(*(exy + i)) = g;
Please tell me how use the operation of pointer.
#define _CRT_SECURE_NO_WARNINGS
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wstrict-prototypes"
#include<stdio.h>
#include<stdlib.h>
#define M 3
#define N 3
int readmoduluskai(filename, time, gcoef, kcoef, ge, ke, dm)
char filename[];
char dm[];
double *time, *gcoef, *kcoef;
double *ge, *ke;
{
FILE *fp;
int i = 0;
printf("Modulus file name : ");
scanf("%s", filename);
if (NULL == (fp = fopen(filename, "r"))) {
printf("\7\n Cannot open file..\n\n");
exit(1);
}
//fscanf(fp, "%12lg", &ge);
//fscanf(fp, "%12lg", &ke);
//printf("%12lg %12lg \n", ge, ke);
while (3 == fscanf(fp, "%lg %lg %lg", time, gcoef, kcoef)) {
printf("%12lg %12lg %12lg \n", *(time), *(gcoef), *(kcoef));
time++;
gcoef++;
kcoef++;
i++;
}
//printf("Please enter the constitutive parameters : ");
//scanf("%s", dm);
fclose(fp);
return i;
}
int readdata(file, t, ex, ey, ez, eyz, ezx, exy, ekk)
char *file;
double *t, *ex, *ey, *ez, *eyz, *ezx, *exy, *ekk;
{
double a, b, c, g;
FILE *fp;
int i = 0;
printf("File name : ");
scanf("%s", file);
if (NULL == (fp = fopen(file, "r"))) {
printf("Cannot open file..\n\n");
exit(1);
}
while (4 == fscanf(fp, "%lg %lg %lg %lg", &a, &b, &c, &g)) {
printf("%12lf %12lf %12lf %12lf \n", a, b, c, g);
(*(t + i)) = a;
(*(ex + i)) = b;
(*(ey + i)) = c;
(*(exy + i)) = g;
i++;
}
fclose(fp);
return i;
}
int main(int argc, char *argv[])
{
char filename[20];
char dm[2];
char file[20];
double *time;
double ptime = 0;
time = &ptime;
double *gcoef;
double pgcoef = 0;
gcoef = &pgcoef;
double *kcoef;
double pkcoef = 0;
kcoef = &pkcoef;
double *ge;
double pge = 0;
ge = &pge;
double *ke;
double pke = 0;
ke = &pke;
double *t;
double pt = 0;
t = &pt;
double *ex;
double pex = 0;
ex = &pex;
double *ey;
double pey = 0;
ey = &pey;
double *ez;
double pez = 0;
ez = &pez;
double *eyz;
double peyz = 0;
eyz = &peyz;
double *ezx;
double pezx = 0;
ezx = &pezx;
double *exy;
double pexy = 0;
exy = &pexy;
double *ekk;
double pekk = 0;
ekk = &pekk;
time = (double *)malloc(sizeof(double));
gcoef = (double *)malloc(sizeof(double));
kcoef = (double *)malloc(sizeof(double));
ge = (double *)malloc(sizeof(double));
ke = (double *)malloc(sizeof(double));
readmoduluskai(filename, time, gcoef, kcoef, ge, ke, dm);//read the modulus parameters
readdata(file, t, ex, ey, ez, eyz, ezx, exy, ekk);
free(time);
free(gcoef);
free(kcoef);
free(ge);
free(ke);
return 0;
}
how can I change it
I'm rotating a bmp image file in C.
I successfully tested copying image(rotate 0 degree). But, It must be a problem in rotating image. Here is my code
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#pragma warning (disable:4996)
#define FILENAMELENGTH 30
#define MAXROW 512
#define MAXCOL 512
//location of pixels
typedef struct
{
int row;
int col;
}COORDI;
//function declarations
void RotatePixel(float Theta, COORDI org, COORDI before, COORDI* after);
void ReadImage(char* original_image);
void WriteImage(char* rotate_image);
char source[MAXROW][MAXCOL];
char dest[MAXROW][MAXCOL];
COORDI org;
float Theta;
int main(void)
{
char original_image[FILENAMELENGTH];
char rotate_image[FILENAMELENGTH];
printf("image name : ");
scanf("%s", original_image);
//rotate angle(radian)
printf("angle : ");
scanf("%f", &Theta);
printf("after rotate file name : ");
scanf("%s", rotate_image);
org.row = MAXROW / 2;
org.col = MAXCOL / 2;
dest[MAXROW][MAXCOL] = { 0, };
ReadImage(original_image);
WriteImage(rotate_image);
printf("rotate completed!\n");
return 0;
}
void ReadImage(char* original_image)
{
FILE* fp;
int i, j;
fp = fopen(original_image, "rb");
if (fp == NULL)
{
printf("File cannot open\n");
exit(-1);
}
for (i = 0; i < MAXROW; i++)
{
for (j = 0; j < MAXCOL; j++)
fread(&source[i][j], sizeof(unsigned char), 1, fp);
}
printf("image read completed !\n");
fclose(fp);
return;
}
//writing a pixel to file
void WriteImage(char* rotate_image)
{
FILE* fp;
int i, j;
COORDI locn;
COORDI* after = (COORDI*)malloc(sizeof(COORDI));
fp = fopen(rotate_image, "wb");
if (fp == NULL)
{
printf("File cannot open\n");
exit(-1);
}
for (i = 0; i < MAXROW; i++)
{
for (j = 0; j < MAXCOL; j++)
{
locn.row = i;
locn.col = j;
RotatePixel(Theta, org, locn, after);
if (after->col < 0) continue;
if (after->row < 0) continue;
if (after->col >= MAXCOL) continue;
if (after->row >= MAXROW) continue;
dest[after->row][after->col] = source[i][j];
putc(dest[after->row][after->col], fp);
}
}
printf("image write completed !\n");
fclose(fp);
return;
}
//rotate pixels
void RotatePixel(float Theta, COORDI org, COORDI before, COORDI* after)
{
int x, y;
x = before.col - org.col;
y = before.row - org.row;
after->col = (int)(x* cos(Theta) - y*sin(Theta)) + org.col; // x
after->row = (int)(x* sin(Theta) + y*cos(Theta)) + org.row; // y
}
I think writing a pixel to file is a problem. where is the missing point?
#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