Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7565493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:10:10+00:00 2026-05-30T14:10:10+00:00

Point of HW is it should read this from a file: int func(int arg)

  • 0

Point of HW is it should read this from a file:

int func(int arg) { int x = 7; char c = 'a'; arg = x + c; return arg; }

and output this:

func, arg, x, c
//or optionally also the next line
int, char, return

So my issue is on getting question marks on my output for characters that should be appearing, and strcmp is not returning a zero for my if statement which requires it to work (note: I have a lot of comments and printf’s to help me figure out where I am going wrong):

while((d=fgetc(function_file)) != EOF) {
    //start by checking for any defines and just looping until a new line character comes up
    if( d == '#')
        flag = true;

    if((d == '\n') && (flag)){
        //flag which says if I am searching for a newline but only after a define/include 
        //was found will I say the flag is not needed anymore to allow normal parsing
        flag = false;
    } //end of check for a define function

    if( (flag == false) && (d != '#') ) {

        //this is where the main chunk of code goes to do all the parsing
        if((d != ' ') && (d != '\t') && (d !='\n') && (d != '{') && (d != '}') && (d != '(') && (d != ')') && (d != '*') && (d != '=') && (d != '+')) {

            printf("Character read is : %c\n", d);
            start = true;
            temp[count] = c;
            count++;
        }


    }//end of main chunk of code
        if((start == true) && ((d == ' ') || (d == '(') || (d == ')') || (d == '{') || (d == '}'))) {
            //end a string and compare it hear
            if(match == false) {
            temp[count] = '\0';
        printf("String: %s\n", temp);//*********************************DEBUGGING***********

            start = false;
            int compare;
            for(compare = 0; compare < key_counter; compare++) {
                int optimus;
                optimus = strcmp(keywords[compare], temp); //************** ONE OF THE ERRORS IS HERE***************************************?
                if(optimus == 0){
                    //printf("this is actually runnning");//*********************************DEBUGGING***********
                    int len = strlen(temp);
                    bizarro_keywords[bizarro_key_counter] = (char *)malloc(sizeof(char) * (len +1));
                    memcpy(bizarro_keywords[bizarro_key_counter], temp, len +1);
                    printf("\nWhats inside bizarro_key_counter right after it is allocated memory: %s", bizarro_keywords[bizarro_key_counter]);
                    bizarro_key_counter++;
                    match = true;
                }

            }

            int x;
            for(x = 0; x < count; x++)
                temp[x] = '\0';
            count = 0;


        } else { //if match equals true just grab the next available string 
            //printf("is this one ever running?");
            temp[count] = '\0';
            start = false;
            printf("String: %s\n", temp);

                    int len = strlen(temp);
                    identifiers[iden_counter] = (char *)malloc(sizeof(char) * (len +1));
                    memcpy(identifiers[iden_counter], temp, len +1);
                    iden_counter++;
                    match = false;



                    int x;
                    for(x = 0; x < count; x++)
                        temp[x] = '\0';
                    count = 0;

            }   
        }

}//end of while loop for reading the whole file

This is my output:

Character read is : i
Character read is : n
Character read is : t
String: ???
Character read is : f
Character read is : u
Character read is : n
Character read is : c
String: ????
Character read is : i
Character read is : n
Character read is : t
String: ???
Character read is : a
Character read is : r
Character read is : g
String: ???
Character read is : i
Character read is : n
Character read is : t
String: ???
Character read is : x
String: ?
Character read is : 7
Character read is : ;
String: ??
Character read is : c
Character read is : h
Character read is : a
Character read is : r
String: ????
Character read is : c
String: ?
Character read is : '
Character read is : a
Character read is : '
Character read is : ;
String: ????
Character read is : a
Character read is : r
Character read is : g
String: ???
Character read is : x
String: ?
Character read is : c
Character read is : ;
String: ??
Character read is : r
Character read is : e
Character read is : t
Character read is : u
Character read is : r
Character read is : n
String: ??????
Character read is : a
Character read is : r
Character read is : g
Character read is : ;
String: ????

I am new to C, and I am lost as to why I am getting that output. Hints please.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:10:12+00:00Added an answer on May 30, 2026 at 2:10 pm

    From what I understand you just want to read line from the input file and split it into tokens. You could use strtok function instead of reading characters from the file:

    char* keywords[] = { "int", "char", "return" };
    int i = 0, j, keywordsCount = 3;
    FILE* f = fopen("a.txt", "r");
    char line[1000], *token;
    while (fgets(line, 1000, f) != NULL) // read line
    {
        char* token = strtok(line, " \t\n{}()*+=,;");
        while (token != NULL)
        {
            printf("String %d:%s", i++, token);
    
            for (j = 0; j < keywordsCount; ++j)
                if (strcmp(token, keywords[j]) == 0)
                {
                    printf(" <-- Look, it's keyword!");
                    break; // breaks for, not while
                }
    
            putchar('\n');
            token = strtok(NULL, " \t\n{}()*+=,;");
        }
    }
    

    Note that I use '\n' character in delimiters string because fgets function reads line into buffer that will include '\n' at the end.

    Content of file a.txt:

    int func(int arg) { int x = 7; char c = 'a'; arg = x + c; return arg; }
    

    Output:

    String 0:int <-- Look, it's keyword!
    String 1:func
    String 2:int <-- Look, it's keyword!
    String 3:arg
    String 4:int <-- Look, it's keyword!
    String 5:x
    String 6:7
    String 7:char <-- Look, it's keyword!
    String 8:c
    String 9:'a'
    String 10:arg
    String 11:x
    String 12:c
    String 13:return <-- Look, it's keyword!
    String 14:arg
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm convinced at this point that I should be creating subclasses of std::exception for
This is a short question. At some point my thread understand that it should
I am trying to extract the mp3 header from a file. This is different
I've got an ArrayList of objects that I read from a txt file, RegistrationFormBean();
Here is my code: std::string readString() { int strLen = Read<int>(); char* rawString =
I have code which uses the StreamReader to read HTML from a file, then
I am trying to read a syslog repeatedly and only start from the point
Basically, i want to read highscores from a file and check if the user
This is a method I'm using to load saved game data from a file.
I am trying to read a series of names from a text file and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.