Like Tree1Likes
  • 1 Post By Salem

Syntax checking

This is a discussion on Syntax checking within the C Programming forums, part of the General Programming Boards category; This is not homework.. I am just stuck at a place where I have no access to a c compiler ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    Syntax checking

    This is not homework.. I am just stuck at a place where I have no access to a c compiler and I need to verify that this code will actually compile and run before I send it off. Any help would be appreciated.

    Sample data in infile:
    Code:
    1:ABC:07
    2:DEF:10
    2:ABD:05
    1:CDF:12
    Output should be:
    Code:
    outfile1:
    ABC
    CDF
    19
    
    outfile2:
    DEF
    ABD
    15
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    FILE *input_file, *outfile1, *outfile2;
    
    
    int main(void)
    {
        char    file_line[8];
        int    total1, total2;
        char    *temp;
    
    
        input_file = fopen("infile", "rt");
        outfile1 = fopen("outfile1", "wt");
        outfile2 = fopen("outfile2", "wt");
        total1 = 0;
        total2 = 0;
    
    
        if (input_file == NULL)
        {
            printf ("** Data file could not be opened **");
        }
        else
        {
            while(fgets(file_line, 8, input_file) != NULL)
            {
                temp = strtok(file_line, ":");
                if (atoi(temp[0]) == 1)
                {
                    fprintf(outfile1, "%s\n", temp[1]);
                    total1 = total1 + atoi(temp[2]);
                }
                else if (atoi(temp[0]) == 2)
                {
                    fprintf(outfile2, "%s\n", temp[1]);
                    total2 = total2 + atoi(temp[2]);
                }
                else
                {
                    printf("Unexpected data in field 1!\n");
                }
            }
            fprintf(outfile1, "%u\n", total1);
            fprintf(outfile2, "%u\n", total2);
        }
    }
    Last edited by Will Winans; 08-08-2012 at 09:23 PM. Reason: noticed typos

  2. #2
    Registered Boozer
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    1,724
    Not even close.

    You don't want to subscript temp (that gives you a char).
    Instead of temp[0] use temp.
    Instead of temp[1] use temp+1.
    (Actually, your offsets are incorrect anyway.)

    temp+2 will point at the letters (e.g. ABC) but they are not zero-terminated, so you can't just print it out. You need to do a strtok(NULL, ":") to move the nul. But since you need to do that, you may as well use the return value of strtok.
    Code:
    temp = strtok(file_line, ":");
    n = atoi(temp);
    temp = strtok(NULL, ":");
    if (n == 1) {
        fprintf(outfile1, "%s\n", temp);
        temp = strtok(NULL, ":");
        total1 += atoi(temp);
    }
    else if
    ...
    And file_line should have a size of at least 10 (for newline and ascii-nul).
    Last edited by oogabooga; 08-08-2012 at 11:17 PM.
    The human mind treats a new idea the way the body treats a strange protein; it rejects it. - P.B.Medawar

  3. #3
    and the hat of mystery Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    31,330
    codepad

    > I am just stuck at a place where I have no access to a c compiler and I need to verify that this code will actually compile and run before I send it off.
    And as oogabooga, merely submitting something which is syntactically valid doesn't mean it's a pile of bug-ridden crap.
    Click_here likes this.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Syntax Versus Pointer Syntax
    By LyTning94 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2011, 09:56 AM
  2. syntax help!!!!!!
    By 12oclock in forum C Programming
    Replies: 4
    Last Post: 02-22-2008, 04:56 PM
  3. Syntax Help Please
    By misplaced in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2005, 07:47 AM
  4. About syntax and std::everything
    By Cobras2 in forum C++ Programming
    Replies: 13
    Last Post: 06-04-2003, 04:42 PM
  5. syntax
    By ghe1 in forum Linux Programming
    Replies: 4
    Last Post: 02-13-2002, 12:53 PM

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