Silentkiller
Technical User
Hi,
i wrote a program to open and read a file and wanted to store my the content into a buffer. Does my program works? How can i read the data from my buffer?
#include <stdio.h>
#include <string.h>
#define buffer 1000
main()
{ FILE *pf;
int i;
int width;
int height;
int maxVal;
int type;
int data;
int size;
int ch;
char string[80];
char buff[buffer];
printf("Please enter Filename : "
scanf("%s", string);
if((pf=fopen(string, "r") == NULL)
{
printf("Error(1) : Invalid %s File\n", string);
exit(1);
}
ch = getc(pf);
if (ch != 'P')
{
printf("ERROR(2) : Not Valid PGM File type\n"
exit(2);
}
ch = getc(pf);
type = ch - 48;
if (( type != 2) && ( type != 5))
{
printf("EORROR(2): Not Valid PGM File type\n"
}
printf("\n The value is P%ld", type);
while (getc(pf) != '\n');
while (getc(pf) == '#')
{
while(getc(pf) != '\n');
break;
}
fscanf(pf, "%ld", &width);
fscanf(pf, "%ld", &height);
fscanf(pf, "%ld", &maxVal);
printf("\n Width = %d", width);
printf("\n Height = %d", height);
printf("\n MaxVal = %d\n", maxVal);
size = width*height;
while (getc(pf) != '\n');
for(i=0; i<=size; i++)
{
fscanf(pf,"%ld",&data); // problem section
strcpy(buff,string); // problem section
printf("%ld", buff); // problem section
}
fclose(pf);
printf("\n"
return(0);
}
i wrote a program to open and read a file and wanted to store my the content into a buffer. Does my program works? How can i read the data from my buffer?
#include <stdio.h>
#include <string.h>
#define buffer 1000
main()
{ FILE *pf;
int i;
int width;
int height;
int maxVal;
int type;
int data;
int size;
int ch;
char string[80];
char buff[buffer];
printf("Please enter Filename : "
scanf("%s", string);
if((pf=fopen(string, "r") == NULL)
{
printf("Error(1) : Invalid %s File\n", string);
exit(1);
}
ch = getc(pf);
if (ch != 'P')
{
printf("ERROR(2) : Not Valid PGM File type\n"
exit(2);
}
ch = getc(pf);
type = ch - 48;
if (( type != 2) && ( type != 5))
{
printf("EORROR(2): Not Valid PGM File type\n"
}
printf("\n The value is P%ld", type);
while (getc(pf) != '\n');
while (getc(pf) == '#')
{
while(getc(pf) != '\n');
break;
}
fscanf(pf, "%ld", &width);
fscanf(pf, "%ld", &height);
fscanf(pf, "%ld", &maxVal);
printf("\n Width = %d", width);
printf("\n Height = %d", height);
printf("\n MaxVal = %d\n", maxVal);
size = width*height;
while (getc(pf) != '\n');
for(i=0; i<=size; i++)
{
fscanf(pf,"%ld",&data); // problem section
strcpy(buff,string); // problem section
printf("%ld", buff); // problem section
}
fclose(pf);
printf("\n"
return(0);
}