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 8533655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:01:02+00:00 2026-06-11T10:01:02+00:00

I’m trying to read information from a file and process that information in a

  • 0

I’m trying to read information from a file and process that information in a certain way. I need to make an array of all the words on the very left hand side of the file that don’t have white space in front of them. I keep however getting really odd output when I try to display the contents of that char array.

Here is the sample input:

# Sample Input

    LA 1,3
    LA 2,1
TOP    NOP
    ADDR 3,1
    ST 3, VAL
    CMPR 3,4
    JNE TOP
    P_INT 1,VAL
    P_REGS
    HALT
VAL     INT 0
TAN     LA  2,1

So for instance when I run my program, my output should be:

TOP
VAL
TAN

Instead I’m getting:

a
aTOP
aVAL
aTAN
a
a

I’m not sure why this is happening. Any minor changes I make don’t actually help, they just change what’s in front of my expected output. Sometimes it’s ASCII value 0 or 20 characters. Hopefully someone can help me fix this because it’s driving me crazy.

Here’s my code:

#include <string>
#include <iostream>
#include <cstdlib>
#include <string.h>
#include <fstream>
#include <stdio.h>



using namespace std;


int main(int argc, char *argv[])
{
// If no extra file is provided then exit the program with error message
if (argc <= 1)
{
    cout << "Correct Usage: " << argv[0] << " <Filename>" << endl;
    exit (1);
}

// Array to hold the registers and initialize them all to zero
int registers [] = {0,0,0,0,0,0,0,0};

string memory [16000];

string symTbl [1000][1000];

char line[100];
char label [9];
char opcode[9];
char arg1[256];
char arg2[256];
char* pch;

// Open the file that was input on the command line
ifstream myFile;
myFile.open(argv[1]);


if (!myFile.is_open())
{
    cerr << "Cannot open the file." << endl;
}

int counter = 0;
int i = 0;

while (myFile.good())
{
    myFile.getline(line, 100, '\n');

    // If the line begins with a #, then just get the next line
    if (line[0] == '#')
    {
        continue;
    }


    // If there is a label, then this code will run

    if ( line[0] != '\t' && line[0]!=' ')
    {
        if( pch = strtok(line-1," \t"));
            {
                strcpy(label,pch);
                cout << label << endl;
            }

        if (pch = strtok(NULL, " \t"))
        {
            strcpy(opcode,pch);
        }

        if (pch = strtok(NULL, " \t,"))
        {
            strcpy(arg1,pch);
        }

        if (pch = strtok(NULL, ","))
        {
            strcpy(arg2, pch);
        }
    }


}



return 0;
}
  • 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-06-11T10:01:03+00:00Added an answer on June 11, 2026 at 10:01 am

    You are passing line-1 to strtok, which will cause it to return a pointer to the character before the start of the string; accessing line[-1] will produce undefined behaviour. strtok takes a pointer to the start of a string.

    You’ve also got a ; at the end of your if( pch = strtok(line-1," \t")) statement, which nullifies the if test and causes the block to run even if pch is NULL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to

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.