Grouping two related lines together from a file...help!

This is a discussion on Grouping two related lines together from a file...help! within the C Programming forums, part of the General Programming Boards category; Hi, if I have a file that looks like this -------------------- 8 ihave 22 a 0 aheadache --------------------- where the ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    6

    Grouping two related lines together from a file...help!

    Hi, if I have a file that looks like this

    --------------------

    8
    ihave
    22
    a
    0
    aheadache

    ---------------------
    where the first line is an integer less than 100, and the next line are some letters less than 1000.
    The first two lines (8 and ihave, for example) go together (8 is like the number of times the word ihave appears). Is there a way I can store these two together and then read the next two and store them together and then the next and so on?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Use one fgets() call to read the first line into a buffer. Use another fgets() call to read the next line into another buffer. Then do whatever you need to with those two buffers.

    Repeat until end of file.

    Code:
    char b1[BUFSIZ], b2[BUFSIZ];
    while ( fgets( b1, BUFSIZ, fp ) != NULL ) {
      if ( fgets( b2, BUFSIZ, fp ) != NULL ) {
        // do stuff with b1 and b2
      }
    }

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    6
    Okok, I used that method...
    with this file
    ------------------------
    32
    abcdefg
    22
    hijklm
    45
    nopqr

    ------------------------

    this code:
    Code:
    #include <stdio.h>
    #define MAX 100
    #define SOURCE2 "pattern2.txt"
    
    int main(void)
    {
    	
    	char b1[MAX]; 
    	char b2[MAX];
    	FILE *fp = fopen(SOURCE2, "r");
    
    	while ( fgets( b1, MAX, fp ) != NULL ) {
      	if ( fgets( b2, MAX, fp ) != NULL ) {
        	printf("%s", b1);
    	printf("%s", b2);
      	}
    	}
    
    	printf("\nb1[0] = %c", b1[0]);
    	printf("\nb2[0] = %c", b2[0]);
    
    	fclose(fp);
    
    	return 0;
    	
    }
    and got this output:

    ------------------------------
    32
    abcdefg
    22
    hijklm
    45
    nopqr

    b1[0] =

    b2[0] = n
    ------------------------------

    It works inside the 'if' function but i dont really want to anything within the if function (if thats possible). I want the numbers 32 or 22 or 45 to stay as numbers so i can do comparisons and formulas with them much later on. And I want 32 to be associated with abcdefg.

    It says its my main() function, but i actually want to make it into a function that can be called by my main function, so that it can return a number (32) and an array of characters (abcdefg). And then get another pair and return it and then another pair...up a around 50 -100 pairs(i just know its alot of pairs).

    Sorry, your method may work for what im trying to do but im a supernewb at this stuff, so any explanations and further help would be good, thanks!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    You need to remove the newline from the end of b1
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Then you should be able to print
    printf( "%s %s", b1, b2 );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21