Thread: Problem with reading data files

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    9

    Problem with reading data files

    I'm confused as to how I can read a file like this: (any help/advice would be greatly appreciated! Thanks)

    Params
    0
    1
    2
    3
    4
    1,3
    Data
    3,2
    4,4
    5,5
    3,2
    0,0

    I've gotten this far with a program to do it:
    Code:
    #include <stdio.h>
    
    int main()
    
    {
    	float G, vi, B, t, duration[], burnrate[];
    	int mi, ve, D, S;
    		
    	FILE *inFile, *outFile;
    	inFile = fopen("LAND2.txt", "r");			
    	if (inFile == NULL)
    	{
    	printf("\nFailed to open the file.\n");			exit (1);
    	}
    		
    	if (inFile != NULL)
    	{
    	printf("\nThe file LAND2.txt was opened successfully.");
    	}
    
    	while (fscanf(inFile, "%f", . . . )
    
       	fscanf (inFile, "Params \n");				fscanf (inFile, "%f", &G \n); 			
    	fscanf (inFile, "%f", &vi \n);				fscanf (inFile, "%f", &B \n);				fscanf (inFile, "%f", &t \n);				fscanf (inFile, "%d", &mi \n);			
    	fscanf (inFile, "%d", &ve \n);				fscanf (inFile, "%d, %d", &D, &S);		
    
    	fscanf (inFile, "Data \n");   	
    
    	for (i = 0; i < 5; ++i)
    	fscanf (inFile, "%d,%f", &duration[i], &burnrate[i] &B \n);
    	}
    	
    fclose (inFile);
    }

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Well, what problems are you having?

    I don't think fscanf() bothers with locale settings so decimal commas for floating point numbers might not work as expected.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    I just am not sure what should go inside the "while (fscanf(inFile, "%f", . . . )" bit .. . i haven't even made it far enough to run my program yet.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    sorry, since some of the data has more than one value per line.

    should it be while (fscanf(inFile, "%f", &x) . . . and how would i let the 'x' change for each variable i put in

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  2. Problem in reading data
    By serena in forum C Programming
    Replies: 2
    Last Post: 05-04-2005, 08:37 PM
  3. Reading files in a directory
    By roktsyntst in forum Windows Programming
    Replies: 5
    Last Post: 02-07-2003, 10:04 AM
  4. problem reading pe files
    By Sebastiani in forum C++ Programming
    Replies: 0
    Last Post: 01-26-2003, 11:14 AM
  5. reading data from disk into array problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 03:19 PM