DragXSlay
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.