format in Fortran

This is a discussion on format in Fortran within the C Programming forums, part of the General Programming Boards category; I need to know is there any equivalent of the "format" statement used in Fortran77 in C. For example what ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    11

    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)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    printf

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    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);
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. real-world apps in C++ (preferably simple)?
    By Aisthesis in forum C++ Programming
    Replies: 13
    Last Post: 06-12-2009, 01:03 PM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 05:02 AM
  4. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  5. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 05:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21