[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

Reading Text Files in C and Separating into Different Strings

I have a text file that I'm reading into my C program using while (fgets(line, line_size, sourcefp) != NULL).  A sample of the test file is below.  Basically, columns 1-7 is the label, 8-15 is the opcode, 16-27 is the operand, 28 is blank, and 29-66 is the comment (Note: The entire line is ignored if the first character is a period(.)).  By using the fgets statement above, I'm trying to take substrings out of the string line and assign them to separate string variables.  I do this using:

strncpy(label, line, 7);
label[7]='\0';
strncpy(mnemonicOpCode, line + 8, 7);
mnemonicOpCode[7]='\0';
strncpy(operand, line + 15, 13);
operand[13]='\0';
if ( strlen(line) > 28) {
     strcpy(comment,&line[28]);
     comment[30]='\0';
}

However, this isn't working for me.  The problem I have is that there may or may not be a comment.  If there is a comment, I can copy all of columns 16-27 into operand.  If there is no comment, I can only copy the columns I need into operand.  I cannot copy the newline character into any of these fields those.  Does anyone know a way to not only copy the fields into these string variables properly but also trim any whitespace character from them?  Any help on this is appreciated.  I don't necessarily have to use fgets either if someone knows a better way.  Thanks in advance!



COPY    START   0           COPY FILE FROM INPUT TO OUTPUT
FIRST   STL     RETADR      SAVE RETURN ADDRESS
        LDB    #LENGTH      ESTABLISH BASE REGISTER
        BASE    LENGTH
CLOOP  +JSUB    RDREC        READ INPUT RECORD
        LDA     LENGTH      TEST FOR EOF (LENGTH = 0)
        COMP   #0
        JEQ     ENDFIL      EXIT IF EOF FOUND
       +JSUB    WRREC       WRITE OUTPUT RECORD
        J       CLOOP       LOOP
ENDFIL  LDA     EOF         INSERT END OF FILE MARKER
        STA     BUFFER
        LDA    #3           SET LENGTH = 3
        STA     LENGTH
        J      @RETADR
0
DragXSlay
Asked:
DragXSlay
1 Solution
 
KocilCommented:
// Assume that your field always start at the right
// position, these function may suffice

// copy string until SPACE, NL or NUL
// return last char
char strleftcpy(char *dest, char *source)
{
   while ((*source != '\0') &&
          (*source != '\n') &&
          (*source != ' ')) {
       *dest = *source;
       dest++; source++;
   }
   *dest = 0;
   return *source;
}

Then you can use that function like these
strleftcpy(label, line);
strleftcpy(mnemonicOpCode, line + 8);
strleftcpy(operand, line + 15);
if (strlen(line) > 28) {
    strcpy(comment,line + 28);
}

0

Featured Post

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now