?
Solved

Reading Text Files in C and Separating into Different Strings

Posted on 2003-02-22
1
Medium Priority
?
323 Views
Last Modified: 2011-09-20
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
Comment
Question by:DragXSlay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 5

Accepted Solution

by:
Kocil earned 200 total points
ID: 8000905
// 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question