4 Replies - 1666 Views - Last Post: 30 October 2009 - 12:10 PM Rate Topic: -----

#1 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

Reading data from a text file

Posted 27 October 2009 - 08:00 AM

my C code is to read data from a text file that contains 120 rows and 6 columns numbers which between 1 and 45. I want all the numbers are to be stored into a 2D array as number but not a character, for later math calculation purposes. however i got a strange output, i can't get the exact numbers between 1 and 45 from the text file to store into an array. as i know i am unable to get the ascii code right. kindly to correct my code below.

char jp[row][col];
char ch;

while ((ch = getchar()) != EOF) {
		  if (ch >= 1 && ch <= 45)
			 jp[i][j++] = ch;
		  if (j == col) {
			 ++i;
			 j = 0;
		  }
	}



besides that, how do i read from and write data into a text file simultaneously? at the command prompt i enter filename <filename.txt to read data into the code, if i want the output to be written into a text or doc file, what should i enter at the command prompt for reading and writing at the same time? reading and writing are 2 separate files. thanks for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading data from a text file

#2 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Reading data from a text file

Posted 27 October 2009 - 10:24 AM

View Postkckc314, on 27 Oct, 2009 - 09:00 AM, said:

char jp[row][col];
char ch;

while ((ch = getchar()) != EOF) {
		  if (ch >= 1 && ch <= 45)
			 jp[i][j++] = ch;
		  if (j == col) {
			 ++i;
			 j = 0;
		  }
	}



Ummm... in a text file the number 102, is represented by 3 characters. What your code is assigning the decimal value of the character '1' (49) to the array, '0' (48), '3' (51), ' ' (32), '?' (63), etc. etc.
    So if your gonna go about it one character at a time. You may wanna...
  • Zero out the spot in the array
  • Check to make sure char is a digit, >= '0' and <= '9'.
  • If yes : arr[i] *= 10, arr[i] += ch - '0'.
  • If no: i++
  • Get next ch, and repeat from top till EOF.

Or just use...
scanf(" %i ", arr[i][j]); /* Get one int */



View Postkckc314, on 27 Oct, 2009 - 09:00 AM, said:

besides that, how do i read from and write data into a text file simultaneously? at the command prompt i enter filename <filename.txt to read data into the code, if i want the output to be written into a text or doc file, what should i enter at the command prompt for reading and writing at the same time? reading and writing are 2 separate files. thanks for your help.


If you wanna use pipes (redirecting stdin & stdout), command < infile > outfile.
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Reading data from a text file

Posted 27 October 2009 - 10:51 AM

By the way, getchar() returns an int... (it's stated as such in the documentation).
Was This Post Helpful? 0
  • +
  • -

#4 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

Re: Reading data from a text file

Posted 30 October 2009 - 10:16 AM

this part of my code doesn't output anything, i don't know what went wrong. i have checked the array as it has data been stored inside. thx for your help.
for (i = 0; i < row; i++) {
		for (j = 0; j < col; j++) {
			
			low = 0;
			high = 0;
			odd = 0;
			even = 0;
			sum_of_nbr = 0;
			
			sum_of_nbr += jp[i][j];
			
			if (jp[i][j] <= 22)
			   low++;
			else
				high++;
				
			if (jp[i][j] % 2 != 0)
			   odd++;
			else
				even++;
				
			if (j == col - 1) {
				  
				  if (sum_of_nbr < 106 || sum_of_nbr > 170 || low < 2 || low > 4 || high < 2 || high > 4 || 
					 odd < 2 || odd > 4 || even < 2 || even > 4)
					 continue;
					 
					 total_line++;
					 printf("%d  %d  %d  %d  %d  %d", jp[i][0], jp[i][1], jp[i][2], jp[i][3], jp[i][4], jp[i][5]);
					 printf("sum: %u ", sum_of_nbr);
					 if (low == 2 && high == 4)
						printf("L/H: 2/4 ");
					 else
					 if (low == 4 && high == 2)
						printf("L/H: 4/2 ");
					 else
					 if (low == 3 && high == 3)
						printf("L/H: 3/3 ");
						
					 if (odd == 2 && even == 4)
						printf("O/E: 2/4\n");
					 else
					 if (odd == 4 && even == 2)
						printf("O/E: 4/2\n");
					 else
					 if (odd == 3 && even == 3)
						printf("O/E: 3/3\n");
			}
		}
	}


Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,757
  • Joined: 19-February 09

Re: Reading data from a text file

Posted 30 October 2009 - 12:10 PM

You appear to be initialising the variables within the loop:

for (i = 0; i < row; i++) {
		for (j = 0; j < col; j++) {
			
			low = 0;
			high = 0;
			odd = 0;
			even = 0;
			sum_of_nbr = 0;
			
			sum_of_nbr += jp[i][j];


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1