delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/10/08:24:50

Date: Wed, 10 Feb 1999 15:21:56 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: north AT iname DOT com
cc: djgpp AT delorie DOT com
Subject: Re: Two newbee questions
In-Reply-To: <79pkgm$ab4$1@nnrp1.dejanews.com>
Message-ID: <Pine.SUN.3.91.990210151242.22376B-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

On Tue, 9 Feb 1999 north AT iname DOT com wrote:

> What is wrong in this program????

Quite a few things, actually.

> for(n=1;(!feof(file));++n){
      ^^^
Arrays in C start with index 0, not 1.

>  fscanf(file,"%[^\t]\t%[^\n]\n",left[n], right[n]);

left[n] is the n-th character of the string left[].  Strings in C are 
actually arrays of char terminated by a null character '\0'.  If you want 
50 strings, you should have declared it like this:

	char left[50][20], right[50][20];

(that's 50 strings each one large ebough to hold 19 characters and the 
terminating null), and read into the strings like this:

	fscanf(file,"%[^\t]\t%[^\n]\n",&left[n][0], &right[n][0]);

>  printf("%d %s\t%s\n", n, left[n], right[n]);

For the %s format, `printf' needs the *address* of a char array, but you 
pass it the *value* of one character from that array.  Again, given the 
above declaration, here's what you should have written

	printf("%d %s\t%s\n", n, &left[n][0], &right[n][0]);

> And by the way, if I put Swedish characters
> in the text file, the program does not put
> out those characters to the stdout.

Swedish characters have their 8th bit set, so you need to declare all 
strings "unsigned char", like this:

	unsigned char left[50][20];

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019