So my code involves reading in a PPM image and then storing it in an array so it can be saved in an new ppm file. Though I think there is a problem with my pointers that means its not actually reading the file. Also my code ends after allocating the memory for the array. Any help is much appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxheight 1080
#define maxwidth 1920
#define RGB_COMPONENT_COLOUR 255
#define pgmtype "P2"
#define ppmtype "P3"
typedef struct
{
int red, green, blue;
} PPMPixel;
typedef struct
{
int x, y;
} PPMImage;
typedef struct
{
int rgb_comp_colour;
char filetype[3];
int height;
int width;
} PPMHead;
/*PPMHead head[3];
{
head[3].filetype;
head[3].height;
head[3].width;
}*/
PPMHead head;
PPMHead* head_ptr = &head;
PPMPixel p;
PPMPixel* p_ptr = &p;
PPMPixel *data; //Defines pointer to PPMPixel
int **Array; //Double pointer defines as a pointer pointing to a pointer that is pointing to an integer
PPMPixel **RGBArray; //Double pointer defines as a pointer pointing to a pointer that is pointing to the PPMPixel structure
FILE *fp;
int r, g, b;
void headercheck ()
{
fscanf(fp, "%s %d %d %d", head.filetype, &head.width, &head.height, &head.rgb_comp_colour);
printf("%s %d %d %d", head.filetype, head.width, head.height, head.rgb_comp_colour);
if (head.width > maxwidth || head.height > maxheight)
{
printf("\tInvalid image size. The maximum value of image is 1920x1080.\n");
printf("\tImage size is %d x %d\n", head.width, head.height);
}
else
{
printf("\tImage size is valid\n");
printf("\tImage size is %d x %d\n", head.width, head.height);
}
if ((strcmp (head.filetype, pgmtype)!=0) && (strcmp (head.filetype, ppmtype)!=0))
{
printf("\tInvalid filetype\n");
}
else
{
if(strcmp (head.filetype, pgmtype)==0)
{
printf("\t File is PGM type image\n");
}
else
{
if(strcmp (head.filetype, ppmtype)==0)
{
printf("\t File is PPM type image\n");
}
}
}
if ((head.rgb_comp_colour == RGB_COMPONENT_COLOUR))
{
printf("\t Image is 8 bit\n");
}
else
{
if (head.rgb_comp_colour > RGB_COMPONENT_COLOUR)
{
printf("Maximum bit-depth is 8 bits\n");
}
else
{
printf("\tImage is not 8 bit\n");
}
}
}
int main(void)
{
char fname[100];
printf("Enter file name: ");
scanf("%s", fname);
fseek(stdin,0,SEEK_END);
fp = fopen(fname, "r");
if (fp == NULL)
{
printf("\tError while opening the file\n");
}
else
{
printf("\tReading in %s\n", fname);
}
headercheck();
if (strcmp (head.filetype, ppmtype)==0)
{
RGBArray = (PPMPixel **)malloc(head.height*sizeof(PPMPixel*)); //Points to malloc
if((RGBArray == NULL))
{
printf("Error allocating memory to the array");
}
else
{
printf("Memory allocated to the PPM array sucessfully");
}
for (int i=0;i<head.width;i++)
{
RGBArray[i] = (PPMPixel *)malloc(head.width*sizeof(PPMPixel));
}
printf("Error 2");
//Initialising each element
for (int j=0;j<head.height;j++)
{
for (int i=0;i<head.width;i++)
{
fscanf(fp, "%3d %3d %3d ", &p.red, &p.green, &p.blue); //Scans in integers of the address pointer to PPMPixel
data = &RGBArray[i][j]; //Defines data pointer pointing to address of RGBArray[i][j]
data->red = p.red; //Access member of PPMPixel structure to equal one of the three RGB channels
data->green = p.green;
data->blue = p.blue;
}
}
}
fclose(fp);
//Save PPM Array Into New PPM File
FILE *pf;
int i, j;
char fname2[100];
printf("Enter file name: ");
scanf("%s", fname2);
fseek(stdin,0,SEEK_END);
pf = fopen(fname2, "w");
if (pf == NULL)
{
printf("\tError while opening the file\n");
}
else
{
printf("\tWriting in %s\n", fname2);
}
for(j=0;j<head.height;j++)
{
fprintf(pf, "\n");
for(i=0;i<head.width;i++)
{
fprintf(pf, "%3d ", RGBArray[i][j].red);
fprintf(pf, "%3d ", RGBArray[i][j].green);
fprintf(pf, "%3d ", RGBArray[i][j].blue);
//fprintf(pf, "%3d ", (RGBArray+j*head.width + i)*r);
//fprintf(pf, "%3d ", (RGBArray+j*head.width + i)*g);
//fprintf(pf, "%3d ", (RGBArray+j*head.width + i)*b);
}
}
fclose(pf);
free(RGBArray);
RGBArray = NULL;
for(int i=0;i<head.width;i++)
{
free(RGBArray[i]);
RGBArray[i] = NULL;
}
return 0;
}
You mainly mixed width and height, but there are other issues.
width is the number of columns
height is the number of rows
The C two dimensional array in memory order is Row Major.
The convention for storing an image in memory is "by rows".
The indexing is RGBArray[col][row] (in your code it should be RGBArray[j][i]).
Illustration:
RGBArray[0][0] RGBArray[0][14]
| |
V V
<--- width --->
RGBArray[0] -> ############### ^
RGBArray[1] -> ############### |
RGBArray[2] -> ############### height
RGBArray[3] -> ############### |
RGBArray[4] -> ############### V
I used the following PPM text file for testing (in.ppm):
P3
# ppm comment
4 3
255
1 2 3 11 12 13 21 22 23 31 32 33
101 102 103 111 112 113 121 122 123 131 132 133
201 202 203 211 212 213 221 222 223 231 232 233
Corrected code (please read the comments):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxheight 1080
#define maxwidth 1920
#define RGB_COMPONENT_COLOUR 255
#define pgmtype "P2"
#define ppmtype "P3"
typedef struct
{
int red, green, blue;
} PPMPixel;
typedef struct
{
int x, y;
} PPMImage;
typedef struct
{
int rgb_comp_colour;
char filetype[3];
int height;
int width;
} PPMHead;
PPMHead head;
PPMHead* head_ptr = &head;
PPMPixel p;
PPMPixel* p_ptr = &p;
PPMPixel *data; //Defines pointer to PPMPixel
int **Array; //Double pointer defines as a pointer pointing to a pointer that is pointing to an integer
PPMPixel **RGBArray; //Double pointer defines as a pointer pointing to a pointer that is pointing to the PPMPixel structure
FILE *fp;
int r, g, b;
void headercheck()
{
char tmp[201]; //Temporary string.
fscanf(fp, "%2s", head.filetype); //%2s ensures that no more than two characters are read.
fscanf(fp, "%200s", tmp);
if (tmp[0] == '#')
{
//The second line may be a comment starting with '#'
fgets(tmp, 200, fp); //Skip the comment.
fscanf(fp, "%d", &head.width);
}
else
{
//If not a comment, read width from tmp.
sscanf(tmp, "%d", &head.width);
}
//fscanf(fp, "%s %d %d %d", head.filetype, &head.width, &head.height, &head.rgb_comp_colour);
fscanf(fp, "%d %d", &head.height, &head.rgb_comp_colour);
printf("%s %d %d %d", head.filetype, head.width, head.height, head.rgb_comp_colour);
if (head.width > maxwidth || head.height > maxheight)
{
printf("\tInvalid image size. The maximum value of image is 1920x1080.\n");
printf("\tImage size is %d x %d\n", head.width, head.height);
}
else
{
printf("\tImage size is valid\n");
printf("\tImage size is %d x %d\n", head.width, head.height);
}
if ((strcmp(head.filetype, pgmtype) != 0) && (strcmp(head.filetype, ppmtype) != 0))
{
printf("\tInvalid filetype\n");
}
else
{
if (strcmp(head.filetype, pgmtype) == 0)
{
printf("\t File is PGM type image\n");
}
else
{
if (strcmp(head.filetype, ppmtype) == 0)
{
printf("\t File is PPM type image\n");
}
}
}
if ((head.rgb_comp_colour == RGB_COMPONENT_COLOUR))
{
printf("\t Image is 8 bit\n");
}
else
{
if (head.rgb_comp_colour > RGB_COMPONENT_COLOUR)
{
printf("Maximum bit-depth is 8 bits\n");
}
else
{
printf("\tImage is not 8 bit\n");
}
}
}
int main(void)
{
const char *fname = "in.ppm";
//char fname[100];
//printf("Enter file name: ");
//scanf("%s", fname);
//fseek(stdin, 0, SEEK_END);
fp = fopen(fname, "r");
if (fp == NULL)
{
printf("\tError while opening the file\n");
}
else
{
printf("\tReading in %s\n", fname);
}
headercheck();
if (strcmp(head.filetype, ppmtype) == 0)
{
RGBArray = (PPMPixel **)malloc(head.height * sizeof(PPMPixel*)); //Points to malloc
if ((RGBArray == NULL))
{
printf("Error allocating memory to the array\n");
}
else
{
printf("Memory allocated to the PPM array successfully\n");
}
//for (int i = 0; i < head.width; i++)
for (int i = 0; i < head.height; i++) //Iterate height rows
{
RGBArray[i] = (PPMPixel *)malloc(head.width * sizeof(PPMPixel));
if ((RGBArray[i] == NULL))
{
printf("Error allocating memory to the array\n");
}
}
//printf("Error 2");
//Initializing each element
for (int j = 0; j < head.height; j++)
{
for (int i = 0; i < head.width; i++)
{
fscanf(fp, "%3d %3d %3d ", &p.red, &p.green, &p.blue); //Scans in integers of the address pointer to PPMPixel
//data = &RGBArray[i][j]; //Defines data pointer pointing to address of RGBArray[i][j]
data = &RGBArray[j][i]; //The row index comes first - (use [j][i] instead of [i][j])
data->red = p.red; //Access member of PPMPixel structure to equal one of the three RGB channels
data->green = p.green;
data->blue = p.blue;
}
}
}
fclose(fp);
//Save PPM Array Into New PPM File
FILE *pf;
int i, j;
//char fname2[100];
//printf("Enter file name: ");
//scanf("%s", fname2);
//fseek(stdin, 0, SEEK_END);
const char *fname2 = "out.ppm";
pf = fopen(fname2, "w");
if (pf == NULL)
{
printf("\tError while opening the file\n");
}
else
{
printf("\tWriting in %s\n", fname2);
}
//Write the PPM header
////////////////////////////////////////////////////////////////////////////
fprintf(fp, "%s\n", head.filetype);
fprintf(fp, "%d %d\n", head.width, head.height);
fprintf(fp, "%d\n", head.rgb_comp_colour);
////////////////////////////////////////////////////////////////////////////
for (j = 0; j < head.height; j++)
{
//fprintf(pf, "\n");
for (i = 0; i < head.width; i++)
{
//fprintf(pf, "%3d ", RGBArray[i][j].red);
//fprintf(pf, "%3d ", RGBArray[i][j].green);
//fprintf(pf, "%3d ", RGBArray[i][j].blue);
fprintf(pf, "%3d ", RGBArray[j][i].red); //row index comes first - use [j][i] instead of [i][j]
fprintf(pf, "%3d ", RGBArray[j][i].green);
fprintf(pf, "%3d ", RGBArray[j][i].blue);
}
fprintf(pf, "\n");
}
fclose(pf);
//free(RGBArray);
//RGBArray = NULL;
for (int i = 0; i < head.height; i++)
{
free(RGBArray[i]);
RGBArray[i] = NULL;
}
free(RGBArray); //Free RGBArray after free RGBArray[i]
RGBArray = NULL;
return 0;
}
Output file (out.ppm):
P3
4 3
255
1 2 3 11 12 13 21 22 23 31 32 33
101 102 103 111 112 113 121 122 123 131 132 133
201 202 203 211 212 213 221 222 223 231 232 233
Related
//here's the structure
typedef struct student
{
int rno;
char name[20];
struct subject
{
int scode;
char sname[20];
int mark;
} sub[3];
int total;
float per;
} student;
student s1;
FILE *fp;
int j;
fp = fopen("mystudents.txt", "r");
while (fread(&s1, sizeof(student), 1, fp))
{
printf("\n%d \n%s", s1.rno, s1.name);
for (j = 0; j < 3; j++)
{
printf("\n%d", s1.sub[j].mark);
}
printf("\n%d", s1.total);
}
fclose(fp);
Content of my file :
101 brian 23 45 56 124
102 abhi 32 78 90 200
fread() is for reading binary files, not text files (unless you're reading into a string variable). You can use fscanf() to parse the text file.
student s1;
FILE *fp;
fp = fopen("mystudents.txt", "r");
while (fscanf(fp, "%d %s %d %d %d %d", &s1.rno, s1.name, &s1.sub[0].mark, &s1.sub[1].mark, &s1.sub[2].mark, &s1.total) > 0)
{
printf("\n%d \n%s", s1.rno, s1.name);
for (int j = 0; j < 3; j++)
{
printf("\n%d", s1.sub[j].mark);
}
printf("\n%d", s1.total);
}
fclose(fp);
I believe I have a problem with my structure pointer (head), but I'm not sure where I'm doing wrong. It reads the data from the file fine for the headercheck function so I have no clue if that's it or not. I would rather have the structure pointer global, because this is part of a very big program with switch case statements and all that, but it doesn't like that.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxheight 1080
#define maxwidth 1920
#define RGB_COMPONENT_COLOUR 255
#define pgmtype "P2"
#define ppmtype "P3"
typedef struct {
int x, y;
}
PPMImage;
typedef struct {
int rgb_comp_colour;
char filetype[2];
int height;
int width;
}
PPMHead;
/*PPMHead head[3];
{
head[3].filetype;
head[3].height;
head[3].width;
}
PPMHead *ptr_head = NULL;
ptr_head = head;*/
PPMPixel * data; //Defines pointer to PPMPixel
int ** Array; //Double pointer defines as a pointer pointing to a pointer that is pointing to an integer
PPMPixel ** RGBArray; //Double pointer defines as a pointer pointing to a pointer that is pointing to the PPMPixel structure
FILE * fp;
int r, g, b;
void headercheck() {
PPMHead * ptr_head, head;
ptr_head = & head;
fscanf(fp, "%s %d %d %d", head.filetype, & head.width, & head.height, & head.rgb_comp_colour);
printf("%s %d %d %d", head.filetype, head.width, head.height, head.rgb_comp_colour);
if (head.width > maxwidth || head.height > maxheight) {
printf("\tInvalid image size. The maximum value of image is 1920x1080.\n");
printf("\tImage size is %d x %d\n", head.width, head.height);
} else {
printf("\tImage size is valid\n");
printf("\tImage size is %d x %d\n", head.width, head.height);
}
if ((strcmp(head.filetype, pgmtype) != 0) && (strcmp(head.filetype, ppmtype) != 0)) {
printf("\tInvalid filetype\n");
} else {
if (strcmp(head.filetype, pgmtype) == 0) {
printf("\t File is PGM type image\n");
} else {
if (strcmp(head.filetype, ppmtype) == 0) {
printf("\t File is PPM type image\n");
}
}
}
if ((head.rgb_comp_colour == RGB_COMPONENT_COLOUR)) {
printf("\t Image is 8 bit\n");
} else {
if (head.rgb_comp_colour > RGB_COMPONENT_COLOUR) {
printf("Maximum bit-depth is 8 bits\n");
} else {
printf("\tImage is not 8 bit\n");
}
}
}
int main(void) {
PPMHead * ptr_head, head;
ptr_head = & head;
char fname[100];
printf("Enter file name: ");
scanf("%s", fname);
fseek(stdin, 0, SEEK_END);
fp = fopen(fname, "r");
if (fp == NULL) {
printf("\tError while opening the file\n");
} else {
printf("\tReading in %s\n", fname);
}
//Checking for comments
//Skip whitespace and comments
headercheck();
if (strcmp(head.filetype, pgmtype) == 0) {
printf("\tReading in PGM image...\n");
* Array = malloc(head.width * sizeof(int * )); //Points to malloc
for (int i = 0; i < head.width; i++) {
Array[i] = (int * ) malloc(head.height * sizeof(int));
if ((Array = NULL)) {
printf("Error allocating memory to the array");
} else {
printf("Memory allocated to the PGM array sucessfully");
}
}
for (int j = 0; j < head.height; j++) {
for (int i = 0; i < head.width; i++) {
fscanf(fp, "%3d ", & Array[i][j]); //Scans the address of Array[i][j]
}
}
}
fclose(fp);
//Save PPM Array Into New PPM File
FILE * pf;
int i, j;
char fname2[100];
printf("Enter file name: ");
scanf("%s", fname2);
fseek(stdin, 0, SEEK_END);
pf = fopen(fname2, "w");
if (pf == NULL) {
printf("\tError while opening the file\n");
} else {
printf("\tWriting in %s\n", fname2);
}
if (strcmp(head.filetype, pgmtype) == 0) {
for (j = 0; j < head.height; j++) {
fprintf(pf, "\n");
for (i = 0; i < head.width; i++) {
fprintf(pf, "%3d ", Array[i][j]);
//fprintf(pf, "%3d ", **(Array+j*head[0].width + i));
}
}
}
fclose(pf);
for (int i = 0; i < head.width; i++) {
free(Array[i]);
Array[i] = NULL;
}
free(Array);
Array = NULL;
return 0;
}
After reading the material in the text file, if there is a corresponding name through the search, print out the name and money. However, I can only read the first line of the text file. If I want to make it read another line and find the corresponding name what can I do?
my text file has 15 lines.
Jane 50
Bruno 100
Kim 200
Young 150
Will 250
Jane 50
Bruno 100
Kim 200
Young 150
Will 250
Jane 50
Bruno 100
Kim 335
Young 455
Will 555
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct user{
char name [10];
int money;
} userinformation;
int main()
{
userinformation myvar[100];
int x=0;
char find_name[100];
int i = 0;
int idx = 0;
char buffer[1001],*token;
char* ptr;
userinformation* userinfo;
int cents_50=0;
int cents_20=0;
int cents_10=0;
int cents_5=0;
FILE * srcFile = fopen("coins.txt", "r");
if(srcFile == NULL)
{
printf("fail to open file");
return -1;
}
//file read from coins.txt
while (!feof(srcFile))
{
i =0;
fgets(buffer, 1001, srcFile);
token = strtok(buffer, " ");
while (token != NULL)
{
if(i == 0)
{
strcpy(myvar[idx].name, token);;
}
else if (i == 1)
{
myvar[idx].money = atoi(token);
}
i++;
token = strtok(NULL, " ");
}
idx++;
}
for(int i = 0; i <idx; i++)
{
printf("%s %d\n", myvar[i].name, myvar[i].money);
}
fclose(srcFile);
while(x != 2)
{
printf("1.Enter name\n");
printf("2.Exit\n");
scanf("%d%*c", &x);
if(x ==1)
{
printf("Name: ");
scanf("%s%*c", find_name);
for(i=0; i<idx; i++)
{
ptr = strstr(myvar->name, find_name);
}
if(ptr != NULL)
{
printf("Customer: \n");
printf("%s %d\n", myvar->name, myvar->money);
printf("Change: \n");
//calculate 50cents;
if(myvar->money>50)
{
myvar->money/50;
cents_50++;
printf("50 cents : %d \n", cents_50);
}
//calculate 20cents;
if((myvar->money/50)<50 && myvar->money >=20)
{
if((myvar->money%50/20)>=1)
{
(myvar->money%50/20);
cents_20++;
printf("20 cents : %d \n", cents_20);
}
}
//calculate 10cents;
if((myvar->money%50%20/10)<20 && myvar->money >=10)
{
if((myvar->money%50%20/10)>=1)
{
(myvar->money%50%20/10);
cents_10++;
printf("10 cents : %d \n", cents_10);
}
}
if((myvar->money%50%20%10/5)<10 && myvar->money >=5)
{
if((myvar->money%50%20%10/5)>=1)
{
(myvar->money%50%20%10/5);
cents_5++;
printf("5 cents : %d \n", cents_5);
}
}
}
else if(ptr != myvar->name)
{
printf("Not founded\n");
}
}
else if(x ==2)
break;
}
return(0);
}
'''
This seems wrong:
for(i=0; i<idx; i++)
{
ptr = strstr(myvar->name, find_name);
}
myvar is an array but you don't use an array index. In other words - you always use the first element of the array (i.e. myvar[0].name).
Besides that, I would expect that the loop should stop when strstr return a non-NULL value.
Did you intend to do:
for(i=0; i<idx; i++)
{
ptr = strstr(myvar[i].name, find_name);
if (ptr != NULL) break;
}
(btw - do you really want to use strstr? I would expect a string compare using strcmp ).
The same problem, i.e. missing array index, is present in all the code that follows the above code.
BTW: The way you read from the file isn't 100% correct. See Why is “while ( !feof (file) )” always wrong?
I am trying to access all values that I call. I'm not sure if I use array values correctly. In code below I'm trying to write values in file to memory and then call them by address. what is the problem? Am I overwriting on same memory adresses or I call them wrongly? Any help please?
my text file:
0 0 100 500 player1
0 1 400 450 player2
1 1 300 600 player3
FILE *fp;
struct ob {
int x;
int y;
int c_hp;
int max_hp;
char name[100];
int p_id;
};
struct ob *ptr;
int count = 0;
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: program <File name>\n");
exit(1);
}
read_player_values();
}
void read_player_values() {
ptr = (struct ob *)malloc(sizeof(struct ob));
if (ptr == NULL)
printf("out of memory");
else {
while (!feof(fp)) {
count++;
fscanf(fp, "%d%d%d%d%s", &ptr->x, &ptr->y, &ptr->c_hp, &ptr->max_hp,
ptr->name, &ptr->p_id);
ptr->p_id = count;
printf("%d %d %d %d %s %d", ptr->x, ptr->y, ptr->c_hp, ptr->max_hp,
ptr->name, ptr->p_id);
printf("\n");
}
fclose(fp);
printf("------\n");
for (int i = 0; i < count; i++) {
/* here is my problem */
/*trying to print all player names here*/
printf("%s\n", (ptr - i)->name);
}
}
}
Firstly, you have to open the file in main function:
fp = fopen(argv[1],"r");
if (!fp)
return -1;
You can use fgets function to get each line of the file and get all values from this line by using sscanf. After each iteration, you have to re-allocate for ptr. ptr here likes an array that has count elements.
char line_buf[256];
count = 1;
while (fgets(line_buf, sizeof(line_buf), fp)) {
ptr = realloc(ptr, count * sizeof(struct ob));
if(!ptr)
return;
sscanf(line_buf, "%d%d%d%d%s\n", &ptr[count-1].x, &ptr[count-1].y, &ptr[count-1].c_hp, &ptr[count-1].max_hp,
ptr[count-1].name);
ptr[count-1].p_id = count;
printf("%d %d %d %d %s %d", ptr[count-1].x, ptr[count-1].y, ptr[count-1].c_hp, ptr[count-1].max_hp,
ptr[count-1].name, ptr[count-1].p_id);
printf("\n");
count++;
}
For printing all names:
printf("------\n");
for (int i = 0; i < count-1; i++) {
printf("%s\n", ptr[i].name);
}
the result after testing:
#cat text.txt
0 0 100 500 player1
0 1 400 450 player2
1 1 300 600 player3
./test text.txt
0 0 100 500 player1 1
0 1 400 450 player2 2
1 1 300 600 player3 3
------
player1
player2
player3
I have an input file a.txt:
1 abc 3
2 efgh 4.5
3 text 3
4 xyz 2
So basically, it has 3 columns, first one is int, second is text, and third is double. I need to read this file by rows (which actually works, I guess), but have some problems with writing only second and third column to another (b.txt) file. fprinft saves something like this:
0.000000
0.000000
0.000000
0.000000
xvæ$ 0.000000
instead of
abc 3
efgh 4.5
text 3
xyz 2
I simply need to save only the second and the third column from a.txt file to b.txt file. Here's my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct mypair
{
char string[1024];
double number;
} mypair;
void zero_string(char *string, int n)
{
int i;
for(i=0; i<n; i++)
string[i] = '\0';
}
int row(FILE* f, struct mypair *p)
{
int num;
if(!feof(f))
{
if(fscanf(f,"%d %s %lf", &num, p->string, &p->number) == 3)
{
return 0;
}
}
else
{
return 1;
}
}
int main(int argc, char **argv)
{
int n = 5, status = 0, i = 0, j;
struct mypair array[5];
char file_in_name[255];
char file_out_name[255];
FILE *fin;
FILE *fout;
zero_string(file_in_name, 255);
zero_string(file_out_name, 255);
printf("Data file:\n> ");
scanf("%s", file_in_name);
printf("Out file:\n> ");
scanf("%s", file_out_name);
fin = fopen(file_in_name, "r");
fout = fopen(file_out_name, "w");
if( fin == NULL )
{
exit(-1);
}
if( fout == NULL )
{
exit(-1);
}
while(status != 1)
{
status = row(fin, &array[i]);
i ++;
fprintf(fout, "%s %lf\n", array[i].string, array[i].number);
if(i >= n)
break;
}
fclose(fin);
fclose(fout);
for(j=0; j<i; j++)
printf("%s %lf\n", array[i].string, array[i].number);
return 0;
}
I modified the code, now it works, thanks!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct mypair
{
char string[1024];
double number;
} mypair;
void zero_string(char *string, int n)
{
int i;
for(i=0; i<n; i++)
string[i] = '\0';
}
int row(FILE* f, struct mypair *p)
{
int num;
if(!feof(f))
{
if(fscanf(f,"%d %s %lf", &num, p->string, &p->number) == 3)
{
return 0;
}
}
else
{
return 1;
}
}
int main(int argc, char **argv)
{
int n = 5, status = 0, i = 0, j;
struct mypair array[5];
char file_in_name[255];
char file_out_name[255];
FILE *fin;
FILE *fout;
zero_string(file_in_name, 255);
zero_string(file_out_name, 255);
printf("Data file:\n> ");
scanf("%s", file_in_name);
printf("Out file:\n> ");
scanf("%s", file_out_name);
fin = fopen(file_in_name, "r");
fout = fopen(file_out_name, "w");
if( fin == NULL )
{
exit(-1);
}
if( fout == NULL )
{
exit(-1);
}
printf("\n");
while(status != 1)
{
status = row(fin, &array[i]);
if(i >= n)
break;
else
{
if(status != -1)
fprintf(fout, "%s %lf\n", array[i].string, array[i].number);
}
i ++;
}
fclose(fin);
fclose(fout);
for(j=0; j<i; j++)
printf("%s %lf\n", array[j].string, array[j].number);
return 0;
}
In your bottom loop, you want to index your array by j, not i.