-
format in Fortran
I need to know is there any equivalent of the "format" statement used in Fortran77 in C. For example what would be the equivalent of the following statement in C (How is format used in the line with 200?)
Program contbc
parameter (ndwgt=1000,ndpow=1000)
dimension cplot(ndwgt,ndpow)
character*80 project_name
character*80 control_file
open(10,file="setup.dat",form="formatted",
x status="old")
200 format(a)
read(10,200) project_name
write(6,200) project_name
read(10,200) control_file
write(6,200) control_file
read(10,*) nwgt_start,nwgt_end,nwgt_step
write(6,*) nwgt_start,nwgt_end,nwgt_step
read(10,*) pow_start,pow_end,pow_step
write(6,*) pow_start,pow_end,pow_step
close(10)
-
I can't be bothered to google for a definition of what format does in Fortran.
Perhaps you could inform us of what it does, then perhaps we can tell you how to do that in C
-
-
Yes
printf() or fprintf() -- it's a combination of WRITE/FORMAT
scanf() or fscanf() -- it's a combination of READ/FORMAT
although a combination of fgets()/sscanf() would be better.
Code:
int main()
{
// character*80 project_name
// character*80 control_file
char project_name[80];
char control_file[80];
char tmpbuf[80];
int nwgt_start, nwgt_end, nwgt_step;
int pow_start,pow_end,pow_step;
FILE *st; // define your I/O or FILE channel == 10
// open(10,file="setup.dat",form="formatted", status="old")
st = fopen("setup.dat", st);
// 200 format(a)
// read(10,200) project_name
fgets(project_name, 80, st);
// write(6,200) project_name
printf("%s", project_name); // printf=to screen=6, %s="a format"
// read(10,200) control_file
fgets(control_file, 80, st);
// write(6,200) control_file
printf("%s", control_file);
// read(10,*) nwgt_start,nwgt_end,nwgt_step
fgets(tmpbuf, 80, st); // Read the line of ints. %d = int conversion
sscanf(tmpbuf, "%d %d %d", &nwgt_start, &nwgt_end, &nwgt_step);
// write(6,*) nwgt_start,nwgt_end,nwgt_step
printf("%d %d %d", nwgt_start, nwgt_end, nwgt_step);
// read(10,*) pow_start,pow_end,pow_step
fgets(tmpbuf, 80, st); // Read the line of ints. %d = int conversion
sscanf(tmpbuf, "%d %d %d", &pow_start, &pow_end, &pow_step);
// write(6,*) pow_start,pow_end,pow_step
printf("%d %d %d", pow_start,pow_end,pow_step);
// close(10)
fclose(st);
}