LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-06-2010, 02:42 AM   #1
gozlemci
Member
 
Registered: Mar 2010
Location: Turkey
Distribution: Ubuntu 12.04
Posts: 34

Rep: Reputation: 0
Error during reading a file line by line in C


Hi there;
I have a code over there. It reads a line from file and converts contents of it to double.
Code:
/*
 * fileRead.c
 *
 *  Created on: Mar 4, 2010
 *      Author: asimavi
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define CONS_MAX_DIMENSION_X 500
#define CONS_MAX_DIMENSION_Y 500
#define CONS_NULL_VALE -9
#define SAMPLE_FILE "deneme.txt"

//Matrix file element
typedef struct matrix
{

	int height;
	int width;
	double data[CONS_MAX_DIMENSION_Y][CONS_MAX_DIMENSION_X];
}MATRIX;

//Client File element
typedef struct client
{
	int coreNumber;
	MATRIX* matrixFirst;
	MATRIX* matrixSecond;

}CLIENT;

MATRIX Matrix_Buffer [20];
MATRIX Result_Buffer [20];


//Function definitions
void convertToDouble(char *searchBuffer, int yCoordinates,int matrixNumber);
void initialize(char* startBuffer);
void copyString(char *tobeCopied, char *source,int startIndex,int lenght);

int main( int argc, char* argv[] ) {

	 FILE *fp;
	 int i=0;
	 char searchBuffer[100];

	 //Check the avalibility of the program
	 if((fp=fopen(SAMPLE_FILE,"r")) == NULL) {

	    printf("Cannot open file.\n");
	    return(-1);

	  }

	 while(fgets(searchBuffer , 80 , fp) != NULL)
	 {

		 printf("okunan veri:%s - ",searchBuffer);

		 //convert one line to matrix here.
		 convertToDouble(searchBuffer,i,0);

		 i++;

	 }

	 return 0;
}

void convertToDouble(char *searchBuffer,int yCoordinates,int matrixNumber)
{

	char *ptr = (char *)malloc(100 * sizeof (char));
	char *ptr2;
	int start=0;
	int common=0;
	double temp;
	int termCounter=0;

	while(1)
	{
		if(searchBuffer[common] == ',' || searchBuffer[common] == '\0')
		{
			//Another double
			copyString(ptr,searchBuffer,start,common-start);
//			ptr = strncpy( ptr2, &searchBuffer[start], common-start );
			start = common+1;
			temp = atof(ptr);
			Matrix_Buffer[matrixNumber].data[yCoordinates][termCounter] = temp;
			termCounter++;

			if(searchBuffer[common] == '\0')
			{
				break;
			}

		}

		common++;

	}
	initialize(searchBuffer);

}

void copyString(char *tobeCopied, char *source,int startIndex,int lenght)
{

	int i=0;

	while(i<lenght)
	{

		tobeCopied[i++] = source[startIndex++];

	}

}

void initialize(char* startBuffer)
{
	int i=0;

	while(i<99)
	{
		startBuffer[i] = '\0';

	}

}
Assume that below is the input:

44.0 , 67.0 , 15.0 , 65.0
14.0 , 88.0 , 122.0 , 11.0
65.0 , 72.0 , 132.0 , 56.0
57.0 , 62.0 , 22.0 , 176.0

This program only reads the first line of the input file and terminates. It must read whole file, how can I solve that?
I'm using Eclipse IDE for C/C++ Developers and ubuntu as operation system.

Thanks in advance!
 
Old 03-06-2010, 03:05 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,368
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Your initialize function never increments i, so it is stuck in an infinite loop. I haven't validated the rest of the code.

Last edited by neonsignal; 03-06-2010 at 03:06 AM.
 
Old 03-06-2010, 03:20 AM   #3
gozlemci
Member
 
Registered: Mar 2010
Location: Turkey
Distribution: Ubuntu 12.04
Posts: 34

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by neonsignal View Post
Your initialize function never increments i, so it is stuck in an infinite loop. I haven't validated the rest of the code.
I've overlooked this . Problem solved, thank you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ input file reading "line by line" assamite Programming 2 05-31-2008 05:54 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM
File reading line by line and compare Goni Linux - Software 14 09-21-2005 01:24 AM
reading from a file line by line Menestrel Programming 5 12-15-2004 03:17 AM


All times are GMT -5. The time now is 01:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration