Reading Input from file

This is a discussion on Reading Input from file within the C Programming forums, part of the General Programming Boards category; I have a line of text stored as a string, and I scan through it with a loop, when I ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    String

    I have a line of text stored as a string, and I scan through it with a loop, when I reach a space I want to move the word before the space to the back of the line and shift everything forward. Is there a function in c which shifts strings to the left, or cuts off an amount of letters from the front?
    Last edited by Horox; 01-30-2008 at 09:12 PM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,452
    memmove, strcpy - to move regeons
    strchr - to find a character (like space) location in the string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I wrote this program. Pretty straight forward.

    There isn't a built-in function, per se. However, you can achieve what you want to do with selective placement of the null term character and by using two pointers to the array.

    Let's say you have a string you just read in via fgets().

    I like programming\r\n

    You want to first remove the CRLF from the end. (or just LF if you are Unix).
    Now you'll have this: (because you overwrote the line ending(s) with a zero.)

    I like programming\0

    print that line.

    Now, find the first blank. strchr() is very good for that. It will point to the blank between the "I" and the "Like". Variable string is your pointer to the array. And, for conversation purposes, let's say blank_pointer is your second pointer. It points to the blank you just found with strchr().

    Put a \0 where the blank is, like this:

    I\0like programming\0

    Now, you can print out the "like programming" via [i]blank_pointer+1[/b], and follow that by printing the "I" via your string pointer.

    Before you move on to find the next blank, put the blank back between the "I" and "Like". Then, bump up blank_pointer one byte and search for the next blank from blank_pointer on. Repeat until no more blanks are found.

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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