Thread: Adding files of numerical data

  1. #1
    Registered User
    Join Date
    Feb 2006

    Adding files of numerical data

    Hi I was hoping that maybe someone could help me with a small piece of C code. I have a number of files, which are all of similar layout ie. three lines of text and 5-6 columns of numerical data. I need to add each of the elements of the second column in one file to their counterparts in the second column of the other file. I must add them in the ratio of 3:1 so I also need to multiply one column by a scalar. So I need to extract the files (into an array?), manipulate the data, add the array elements into a new array, and return this as a file. I think. Any help anyone could provide would be greatly appreciated as this problem is really holding me up. Thanks very much.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Sydney, Australia
    Which part are you stuck with, and where's your attempt so far?

  3. #3
    Registered User
    Join Date
    Feb 2006
    I'm stuck on the whole thing. I have very little knowledge of C and this problem is the only time i'm going to need it. I've tried reading books on it, but its too difficult starting pretty much from scratch. If you can give me any advice to start me off in the right direction, that'd be great. Thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Break it down into steps
    1. write a program which reads each line from a file and simply prints that line to you.
    This at least means you can do something with the data in the file, if you know you can read it properly
    Typically, this means using fgets() inside a loop (see the FAQ)

    2. expand the program to read from two files at the same time.
    This allows you to check that you would be matching your lines correctly

    3. add code to reject the first 3 lines
    Perhaps some sort of count of the number of lines processed?

    4. add code to convert the 2nd column of the remaining lines into numeric values.
    Print those numeric values to check the conversion is good.

    5. Perform your math, and output the result
    Nearly there now....

  5. #5
    Registered User
    Join Date
    Feb 2006

    awk '{print $1,$2}' G10.47-1.stb
    print out the two columns i need?
    How can i put this into an array though?
    How about something like
    for(i=3; i!=EOF; i++)    #the 1st 3 lines are unnecessary text
    x = array1[i-3];
    Thats not gonna work, is it? I know what i need to do, but i get so bloody confused.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamicly adding data members to structs
    By ITAmember in forum C Programming
    Replies: 11
    Last Post: 06-01-2009, 03:26 PM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  4. Trouble Reading Data from Files
    By CConfusion in forum C Programming
    Replies: 11
    Last Post: 04-06-2006, 07:12 PM
  5. Using fscanf for reading numerical data from text file
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-15-2002, 05:18 PM