Thread: File Manipulation in C?

  1. #1
    Registered User
    Join Date
    Sep 2013

    File Manipulation in C?


    I have some data points (x points) in my program like this:

    double x[10][4]; 
    x[0][0] = 5.1; x[0][1] = 3.5; x[0][2] = 1.4; x[0][3] = 0.2;
    x[1][0] = 4.9; x[1][1] = 3.0; x[1][2] = 1.4; x[1][3] = 0.4;
    x[2][0] = 4.7; x[2][1] = 3.2; x[2][2] = 1.3; x[2][3] = 0.8;
    x[...................and the.............................];
    x[...................other points................];

    As you can see, each point has four coordinates that shows their positions and all I want to do is getting these "x" points' coordinates and their dimension sizes (in this code they have 4 dimension) and number of x points (in this code there is 10 x points) from a data file.

    You can see a data file sample at the below:

    336 5     
    0.49 0.29 0.48 0.5 0.56
    0.07 0.4 0.48 0.5 0.54
    0.56 0.4 0.48 0.5 0.49

    See? It's a data set which has been compiled in GCC. There is totally 336 'x' points in this file, and each point has 7-Dimension so there is seven coordinates which shows each point's position. And at the last, you can see the points' coordinates. I don't want to waste my time with entering these values manually one by one, so I heard that I can use File Manipulation method but I really don't know how. Can somebody show me the way just "with a sample code" please?

    So I want to make program like this automatically:

    double x[336][5]; // 336 is number of data points and 5 is their dimension, I want to get these values from data file. I just wrote 3 of 336 points here.
    x[0][0] = 0.49; x[0][1] = 0.29; x[0][2] = 0.48; x[0][3] = 0.5; x[0][4] = 0.56; // First point's coordinates, I want to get this and all those x values from the data file either.
    x[1][0] = 0,07; x[1][1] = 0.4; x[1][2] = 0.48; x[1][3] = 0.5; x[0][4] = 0.54;
    x[2][0] = 0.56; x[2][1] = 0.4; x[2][2] = 0.48; x[2][3] = 0.5; x[0][4] = 0.49;

    By the way, I'm using a Windows compiler (Bloodshed Dev C++) and these data files compiled in Linux (GCC), so I don't know if there will be a problem when I use these data files in my Windows compiler.

    You can ask me anything, I will answer them with my poor English in pleasure

    Thanks in advance for your help!

  2. #2
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Over the rainbow
    If you don't know anything about parsing a file, I would suggest looking it up on google first. When you find a decent tutorial, come back here and post your attempt to parse the file yourself.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    First, upgrade your windows compiler.
    smorgasbordet - Pelles C
    Orwell Dev-C++ | Free Development software downloads at

    Bloodshed Dev C++ hasn't been maintained for the last decade.

    Next, prepare a simple file to test your code with.
    3 5
    0.49 0.29 0.48 0.5 0.56
    0.07 0.4 0.48 0.5 0.54
    0.56 0.4 0.48 0.5 0.49

    Then you start to write some simple code.
    Really basic code to begin with, like print each line as you read it.
    line=3 5
    line=0.49 0.29 0.48 0.5 0.56
    line=0.07 0.4 0.48 0.5 0.54
    line=0.56 0.4 0.48 0.5 0.49

    Use fopen(), fgets() and fclose()
    If it prints anything else, then you need to fix it before continuing.

    Then start to parse this data, so you can print
    rows = 3, columns = 5
    [0][0] = 0.49 ....

    Come back when you can do that much, then we can talk about arrays to store your data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Manipulation
    By Pr0AHP in forum C Programming
    Replies: 1
    Last Post: 04-11-2010, 10:58 PM
  2. file manipulation help
    By fiveco in forum C Programming
    Replies: 11
    Last Post: 12-09-2009, 04:18 PM
  3. File I/O Manipulation
    By mbh5m in forum C Programming
    Replies: 5
    Last Post: 05-31-2007, 08:11 AM
  4. file manipulation
    By swiss powder in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2002, 12:24 PM
  5. need help with file manipulation
    By angelfly in forum C Programming
    Replies: 0
    Last Post: 09-21-2001, 01:26 PM