Thread: File Line Re-arranging program

  1. #1
    Registered User
    Join Date
    Jul 2007

    File Line Re-arranging program

    I'm writing a small C program to post process a huge txt file produced by a CFD software.

    The CFD creates a "from.txt" file, in which every line contains many parameters calculated by it.
    I need to copy the lines that contain the information i need to another "to.txt" files.

    The problem is that I donīt know how to go thru the lines of the from.txt.
    Once i know how to go thru them it wouldn't be that hard to write the rest of the program.

    I working on something like...

    while(c == getchar());
    if (c == \n);

    ... but i have no idea how to go on...

    I`m a newbie in this world... hope learn a lot from you all!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    while(c == getchar());
    if (c == \n);
    The first line reads a character from stdin and then checks to see if c is equal to the same character that it read. It keeps doing this because you have as semicolon at the end of the while loop. This just makes it spin its wheels until the condition is false.

    The second line checks if c is equal to '\n'. If it is, it does nothing, because you also have a semicolon after that line.

    Look into using fgets() for reading data from a file if you want to read entire blocks of text at a time. If you want to read each character individually, then use fgetc().

  3. #3
    Registered User
    Join Date
    Jun 2007
    Ok then just tell us about the way the information is spread in the file,
    what piece of information you want from the file,
    eg: is it numbers? integers, floats, etc..
    eg: is it strings? Some writen information about something?
    eg: Whatever combination??

    In each case what you should think of is that firstly you should find the line you want, load it in a buffer (use of fread here - > firstly with fseek and ftell find the length of the buffer) and then with other functions we may use try to seperate or get the information you want and save it to other buffer, or whatever. Then you can get the buffer you want and write it down in a new file.

    Hope i helped...

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Cambridge, UK
    1. Open the file in a read mode 
    2. If file open status failure
    3. 	Display error And exit
    4. else
    5. 	Read a line from a file using fgets and store then in a buffer
    6.	check if the buffer contains the paramters which you are looking for?
    7.	if So append the line read onto the to.txt file.
    9.	And again jump on to line 5 till you reach EOF
    This would be simplest solution that you could come out with.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Replies: 2
    Last Post: 11-18-2006, 03:31 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM