Okay, this is not working!
What is wrong with it.
Can somebody point it out to me. I've stared at it for hours.
I have seeked help earlier and he come up with struct but im not really getting it and it doesnt work so.
It's not getting the right calculation. I'm thinking that there might be something wrong with reading the files.
/*
read binary file and calculate parameter of polygon
*/
typedef int16_t points[2];
int main(int argc, char* argv[]) {
FILE* fp = fopen(argv[1], "rb");
// int points[1000];
//long numbytesread=0;
int16_t num_of_coors=0;
//open the files
if( fp == NULL ) {
printf("Could not open.\n");
return -1; // -1 indicates error
}
//read file
fread(&num_of_coors, sizeof(int16_t), 2, fp);
points*points=malloc(sizeof(points)*num_of_coors);
fread(points, sizeof(points), num_of_coors, fp);
//read the array and seperate x coordinates and y coordinates
//calculate using formula (x1-x2)^2+(y1-y2)^2
//need 2 points, 4 coordinates at any single time. read a pair at a time
double sum=0;
int i=0;
//int coors=points[0]*2+1 ;
for(i=1;i<=num_of_coors;i++){
sum+=sqrt(pow((points[i]-points[i+2]),2) + pow((points[i+1]-points[i+3]),2));
}
sum+=sqrt(pow((points[1]-points[num_of_coors-2]),2) + pow((points[2]-points[num_of_coors-1]),2));
printf("The perimeter is %.2lf\n", sum);
fclose(fp);
free(points);
}
I guess the binary format of your file is:
int16_t <num of points>
int16_t <Xcoord> int16_t <Ycoord>
....
First, it is bad way to use same name for type name and for variable name.
Second, you work with 'points' type is incorrect.
The correct code should looks like:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
read binary file and calculate parameter of polygon
*/
typedef int16_t points[2];
int main(int argc, char* argv[]) {
FILE* fp = fopen(argv[1], "rb");
// int points[1000];
//long numbytesread=0;
int16_t num_of_coors=0;
//open the files
if( fp == NULL ) {
printf("Could not open.\n");
return -1; // -1 indicates error
}
//read file
fread(&num_of_coors, sizeof(int16_t), 1, fp);
points* p=malloc(sizeof(points)*num_of_coors);
fread(p, sizeof(points), num_of_coors, fp);
//read the array and seperate x coordinates and y coordinates
//calculate using formula (x1-x2)^2+(y1-y2)^2
//need 2 points, 4 coordinates at any single time. read a pair at a time
double sum=0;
int i=0;
//int coors=points[0]*2+1 ;
for(i=1;i<num_of_coors;i++) {
sum+=sqrt(pow(p[i-1][0]-p[i][0],2)+pow(p[i-1][1]-p[i][1],2));
}
sum+=sqrt(pow(p[0][0]-p[num_of_coors-1][0],2)+pow(p[0][1]-p[num_of_coors-1][1],2));
printf("The perimeter is %.2lf\n", sum);
fclose(fp);
free(p);
return 0;
}
Related
simple C question here!
So I am trying to parse through a string lets say: 1234567W
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
//pointer to open file
FILE *op;
//open file of first parameter and read it "r"
op = fopen("TestCases.txt", "r");
//make an array of 1000
char x[1000];
char y[1000];
//declare variable nums as integer
int nums;
//if file is not found then exit and give error
if (!op) {
perror("Failed to open file!\n");
exit(1);
}
else {
while (fgets(x, sizeof(x), op)) {
//pounter to get the first coordinate to W
char *p = strtok(x, "W");
//print the first 3 digits of the string
printf("%.4sd\n", p);
}
}
return 0;
My output so far shows: "123d" because of the "%.4sd" in the printf function.
I now need to get the next two numbers, "45". Is there a regex expression I can use that will allow me to get the next two digits of a string?
I am new to C, so I was thinking more like "%(ignore the first 4 characters)(print next 2 digits)(ignore the last two digits)"
input: pic
output: pic
Please let me know.
Thanks all.
printf("Next two: %.2s\n", p + 4); should work.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
//pointer to open file
FILE *op;
//open file of first parameter and read it "r"
op = fopen("TestCases.txt", "r");
//make an array of 1000
char x[1000];
char y[1000];
//declare variable nums as integer
int nums;
//if file is not found then exit and give error
if (!op) {
perror("Failed to open file!\n");
exit(1);
}
else {
while (fgets(x, sizeof(x), op)) {
//pounter to get the first coordinate to W
char *p = strtok(x, "W");
//print the first 3 digits of the string
printf("%.4sd\n", p);
printf("Next two: %.2s\n", p + 4);
}
}
return 0;
}
Side note: I added a missing stdio.h include. Please turn on compiler warnings, since this error would've been caught by them.
I am having trouble reading in just a single data point from a file. It is supposed to be able to read two columns of data (such as x and y values), but I found out my code cannot even read a single value of double precision. Any help would be appreciated.
The file is at D:\test.txt
and there is a single value of 1.11111.
Enter the location of file (text file) of the airfoil coordinates: D:\test.txt
There are 1 lines
The amount of data in x and y is 1 points and 1 points.
failed to read.
* Process returned 1 *
Press any key to continue...
That was my input.
/*
Purpose:
Create a program that can take in a list of data points that represents an airfoil from some file.
Then through the use of spline function, spline the data points for interpolation then go on to plotting them.
With these data points, use the Vortex Panel Method to obtain coefficients of lift, pressure, and tangential velocity.
Then after these are calculated, plot each with respect to the splined x data points.
*/
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <stdlib.h>
#define LEN 12
int countlines(FILE *fp);
int main (void)
{
char airfoil[500];
double *x_data = NULL;
double *y_data = NULL;
FILE *pfile = NULL;
int line_count = 0;
double test = 0.0;
printf("Enter the location of file (text file) of the airfoil coordinates: ");
scanf("%s", airfoil);
if(fopen_s(&pfile, airfoil, "r"))
{
printf("Error opening the file for reading the data. Program terminated.\n");
exit(1);
}
line_count = countlines(pfile);
printf("There are %d lines\n", line_count);
x_data = realloc(x_data, line_count*(sizeof(double)));
y_data = realloc(y_data, line_count*(sizeof(double)));
if((!x_data) || (!y_data))
{
printf("Memory allocation has failed. Exiting...\n");
exit(1);
}
printf("The amount of data in x and y is %zu points and %zu points.\n", (sizeof(x_data)/sizeof(double)), (sizeof(y_data)/sizeof(double)));
if(EOF == fscanf_s(pfile, "%lf", &test))
{
printf("failed to read.\n");
exit(1);
}
//for(int i = 0; i < line_count; i++)
//{
//fscanf(pfile, " %lf", &x_data[i]);
//}
printf("The x-data are %lf!\n", test);
//for(int i = 0; i < line_count; i++)
//{
//printf("%.2lf", x_data[i]);
//printf("\n");
//}
return 0;
}
int countlines(FILE *fp)
{
int lines = 0;
char str[LEN];
while(!feof(fp))
{
if (fgets(str, LEN, fp) != NULL);
{
lines++;
}
}
return lines;
}
countlines just brought the file pointer to the end of file. Before you can read anything, you must first rewind the file to the beginning:
fseek(pfile,0,SEEK_SET);
You can do ths in countlines().
See also the comments, that spot some more errors.
I have to open/read a file which is an ascii art file (image)
and requires me to return the width and height of the image to the main routine and then requires me to pass the picture data back via a pointer. Here is the function prototype that I MUST use:
unsigned char *foo(char *filename, int *width, int *height)
Inside of foo, I must use a dynamic array of chars
to store tha image data. I need to use fread() to read
that data. I must also account for the carriage return at the end of each line.
Once I have opened and read the data, pass it back to the main routine. The main routine must then create a dynamic 2D array to store the image, copy the 1D array
into the 2D array, and display the image on the screen.
Image File Name: data.txt
My Code:
#include <stdio.h>
#include <stdlib.h>
void readDimension(FILE *inFile, int *width, int *height)
{
int i;
for (i = 0; i < 2; i++)
{
if (i == 0)
{
fscanf(inFile, "%d", width);
}
if (i == 1)
{
fscanf(inFile, "%d", height);
}
}
}
unsigned char *foo(char *filename, int *width, int *height)
{
FILE *inFile = fopen(filename, "rb");
readDimension(inFile, width, height);
unsigned char *ret = malloc(*width * *height);
fread(ret, 1, *width * *height, inFile);
fclose(inFile);
return ret;
}
int main(int argc, char** argv)
{
FILE *inFile;
int width, height;
unsigned char art;
if (argc == 1)
{
printf("Please specify a file name.\n");
}
else if (argc == 2)
{
inFile = fopen(argv[1], "rb");
if (inFile != NULL)
{
fclose(inFile);
art = foo(argv[1], &width, &height);
int n = sizeof(art);
printf("Data in Array: \\%c \n", art);
printf("Size of Array: %d \n", n);
}
else
{
printf("Error: File Not Found %s", argv[1]);
}
}
printf("Width: %d\n", width); // Testing
printf("Height: %d\n", height); // Testing
}
The problem is that you are trying to get the size of art, which is a pointer. It has fixed size. Size should be computed as width*height:
printf("Size of Array: %d x %d (%d)\n", width, height, width*height);
You declared art as an unsigned char, but you are assigning it a pointer to unsigned char. This is incorrect: art should be declared with an asterisk.
You are also printing a single character %c while passing an array art. This is not going to print anything of interest. If you wish to print the first character, print art[0]. If you wish to print the entire picture, make a pair of nested loops that iterate over the width and height, print each character, and print '\n' after each line.
Here are a few additional notes:
You should not read the entire file in one go with fread, because it would introduce \ns into the data. Instead, you should read the file line by line.
You can read width and height in the same function that reads the data
You do not need a loop to read two integers. You can do it in a single line, like this: fscanf(inFile, "%d %d", width, height);
I'm writing a program that takes in two duplicate PNM P6 files, puts the memory of the first file into a buffer, creates a yellow diagonal line over that, and writes the result to the second file. When I run it, the output file is corrupted and can't be displayed. I noticed when looking at the output that it's missing the three lines that should be at the top:
P6
1786 1344
255
I don't know how to programmatically ensure that those lines stay in the code -- I can't figure out why they're even being overwritten in the first place.
When I manually add those lines to the output, the file is no longer corrupted. However, no yellow diagonal line appears. Is this part of the same issue, or is there something else I should look into fixing?
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
unsigned char red, green, blue;
} iPixel;
typedef struct
{
int x, y;
iPixel *data;
} Image;
Image *
ReadImage(char *filename)
{
FILE *f = fopen(filename, "rb");
Image *img = NULL;
char magicNum[128];
int width, height, maxval;
if (f == NULL)
{
fprintf(stderr, "Unable to open file %s\n", filename);
return NULL;
}
fscanf(f, "%s\n%d %d\n%d\n", magicNum, &width, &height, &maxval);
/*
printf("Magic num = %s width = %d, height = %d, maxval = %d\n",
magicNum, width, height, maxval)
;
*/
if (strcmp(magicNum, "P6") != 0)
{
fprintf(stderr, "Unable to read from file %s, because it is not a PNM file of type P6\n", filename);
return NULL;
}
img = (Image *) malloc(sizeof(Image));
img -> x = width;
img -> y = height;
img -> data = (iPixel*) malloc(img -> x * img -> y * sizeof(iPixel));
fread(img -> data, sizeof(iPixel), width*height, f);
fclose(f);
return img;
}
void WriteImage(Image *img, char *filename)
{
FILE *f = fopen(filename, "wb");
fwrite(img->data, sizeof(iPixel), img-> x * img-> y, f);
fclose(f);
}
Image *
YellowDiagonal(Image *input)
{
int i, j;
for (i = 0; i < input->x; i++)
{
for (j=0; j < input->y; j++)
{
if (i==j)
{
input->data[i].red=255;
input->data[i].green=255;
input->data[i].blue=0;
}
}
}
return input;
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
fprintf(stderr, "Usage: ./3A_c.c <input image file> <output image file>\n");
exit(EXIT_FAILURE);
}
Image *img;
img = ReadImage(argv[1]);
fprintf(stderr, "Read.\n");
YellowDiagonal(img);
fprintf(stderr, "Diagonal line.\n");
WriteImage(img, argv[2]);
fprintf(stderr, "Write.\n");
}
Be careful to write exactly the same format you are reading. The PNM format is well defined, and you are reading it correctly. However, in the writing routine there were a couple of mistakes:
opening a file with "w" or "wb" truncates it to 0 bytes;
best practice is always to check if fopen succeeds;
reading actual ASCII data can be done with fscanf, binary data with fread. Similarly, writing ASCII should be done with fprintf and only binary data again with fwrite.
If you want to make sure you write the same data as you read in before, you need to save it somewhere. The maxval variable is read, but not saved, and so I cannot write it back. However, it is not a huge problem because the rest of your code assumes the image is R8G8B8 anyway, and so maxval should always be 255.
Here is an adjusted WriteImage that works.
void WriteImage(Image *img, char *filename)
{
FILE *f = fopen(filename, "wb");
if (f == NULL)
{
printf ("Unable to open '%s' for writing!\n", filename);
/* better would be: "return -1" to indicate an error, 0 otherwise */
return;
}
fprintf (f, "P6\n");
fprintf (f, "%d %d\n", img->x, img->y);
/* actually you need to write 'maxval' here */
fprintf (f, "%d\n", 255);
fwrite(img->data, sizeof(iPixel), img->x * img->y, f);
fclose(f);
}
With the above out of the way, you can now see your 'diagonal line' is not correct! I'm not going to fix that (I suppose not being able to see what happened stopped you in your tracks), but here are a few pointers just to guide you:
no need to return an Image * if you are changing data in situ anyway
no need to check every single pixel
check the coordinates of what pixels are changed ...
Working with C.
OK so this is probably something obvious but for some reason my program will only print a certain number of values from a .dat input file as opposed to printing all of them. Here's the code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int N = 0;
int j;
float i;
const char Project_Data[] = "FloatValues.dat";
FILE *input = fopen(Project_Data, "r");
if(input != (FILE*) NULL)
{
while(fscanf(input, "%e", &i) == 1)
{
printf("%e\n",i);
++N;
}
printf("\t The number of values in this file is: %d\n", N);
fclose(input);
}
else
printf("Input file could not be read.\n");
return(0);
}
Yeah, so there's about 100000 values or so to be printed yet I only seem to be able to get 20000. The values in the file are ordered sequentially and the compiler only seems to start printing nearer the bottom of the file, after about 80000 or so values.
Anybody know where I'm going wrong?