Building a string char by char

This is a discussion on Building a string char by char within the C Programming forums, part of the General Programming Boards category; Right, what I want to do is read in a file character file by file, and if the character "meets ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Building a string char by char

    Right, what I want to do is read in a file character file by file, and if the character "meets the criteria" (which aren't important here) I want to append it to a buffer. I've tried ever effing way imaginable (to my feeble C-mind), so I'm left with pretty much nothing, except this useless snippet:

    Code:
    while (! feof (f)) {
    	ch = (char*) fgetc (f);
    	/* When I try strncat I get an access violation */
    	strncat (line_buffer, ch, 1);
    }
    I'm really not hot on string processing in C, as you can tell
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    a) It' s bad to use feof to control a loop.
    b) The best way to approach you problem would be to make a separate variable to keep track of where in the line_buffer you are, and just increment it every time you add a character.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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