Thread: Advice reading lines from a text file.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    Advice reading lines from a text file.

    I am tring to figure out the best way to go about doing this.

    Say the user clicks the mouse button or hits a key on the keyboard. It would open a text file, and read a line into the program. Then Separate the number from the message. Then the user clicks the button again. It reads the next line in.

    Not sure how visual novels do it, but that is what I am trying to do here. The number would represent where in the story you are. It would keep track. Then come the message after the number.

    I though about formatting a text file like:
    0 Hello World.
    1 This is a example tutorial done in c.
    2 etc...
    ...
    10 Some text here.

    My first question is would I use something like Fgets() to read the whole file in. Seprate the numbers from the message, store the number into a variable, and store the message into a variable.

    Maby someone on here can show me how to read a line in. Then seprate the number from the string. I understand how to use fgets somewhat. I can do this with it.

    Code:
    #include <stdio.h>
    
    int main() {
      char c[50];
      FILE *file; 
    
      file = fopen("story.txt", "r"); 
    
      if(file==NULL) {
        printf("Can't open story file.\n");
        return 1;
      }
      else {
        printf("Story File Open.\n\n");
        
        while(fgets(c, 50, file)!=NULL) { 
          printf("String: %s", c);        
        }
        fclose(file);
        return 0;
      }
    }
    Now the question remains. If I use the fgets method. I am guessing I write the split code for the number and string inside the while statement. Which C function do I use. I am not to good at strings in C yet. Which C function will take a line, and seprate the interger number from the string, and store them into two variable?

    One more question if I go the Fgets route. What if the story.txt starts to get big. 1000 lines of story lines. Would that take up to much space to store into memory? Maby I can seprate them into multiple story.txt files. Each with a certain numbers of lines.
    Last edited by Fujitaka; 08-11-2009 at 09:19 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Fujitaka View Post
    What if the story.txt starts to get big. 1000 lines of story lines. Would that take up to much space to store into memory?
    There is only one way to really find out

    It's not clear what you want to do with the number. If they are sequential, keep in mind that you don't need them in the file at all, your could just keep count and assign numbers each line read.

    In any case, evidently you want to store the number with the string. For that, you could use a struct:
    Code:
    struct String {
    	int num;
    	char data[256];
    };
    Then a short function to process each line. So a little demo:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct String {
    	int num;
    	char data[256];
    };
    
    
    struct String storyline (char *ptr) {
    	struct String retv;
    	sscanf(ptr,"%d %s",&retv.num, retv.data);
    	return retv;
    }
    
    
    int main(void) {
    	struct String eg = storyline ("1 hello");
    	printf("%d %s\n",eg.num,eg.data);
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Ah. I might not even need that number in the file now. Since when I read the lines in. Line by line. I can grab which line it read, and stick the line number into a variable.

    Normally in a visual noval game. The user clicks the mouse or a keyboard button, and the next part of the story is displayed onto the screen.

    And I should be able to say after reading in so many characters from one line. Have it continue on the next line? using a \n. So that way the text doesn't run outside the set space I will have for it.
    Last edited by Fujitaka; 08-11-2009 at 09:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading issue, mixed text file
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-22-2009, 04:38 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Reading lines from a file
    By blackswan in forum C Programming
    Replies: 9
    Last Post: 04-26-2005, 04:29 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM