Help! reading from a file and output 
Author Message
 Help! reading from a file and output

Hello all!

I have a problem reading characters from a file.  This program is
supposed to read characters from a text file and output to a screen.
However, when I input:

*****
** **
*****
 output is:
*****
** **
***
(star[2][3] and star[2][4] are not printed)
When I tried to find out the problem using "watch","*" was assigned
both arrays, but they are not printed.  I totally don't understand
what wrong is.  Please somebody teach me how to solve the problem.

Thanks! I appreciate any advice.

Ryuji

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

int main(void)
{
  FILE *fp;
  char star[3][5];
  int row1,col1,row2,col2;

if((fp = fopen("star.dat","r")) == NULL)
  {
        fprintf(stderr,"Error");
        exit(1);
  }/*if*/
for(row1=0;row1<3;row1++)
   {
        for(col1=0;col1<5;col1++)

          fscanf(fp,"%c",&star[row1][col1]);
   }/*for*/
 fclose(fp);

for(row2=0;row2<3;row2++)
{
  for(col2=0;col2<5;col2++)
        {
        printf("%c",star[row2][col2]);
        }/*for*/

Quote:
}/*for*/

return 0;
Quote:
}



Tue, 30 Nov 1999 03:00:00 GMT  
 Help! reading from a file and output

This is a multi-part message in MIME format.

------=_NextPart_000_01BC7862.A5DA3340
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit



Quote:
> Hello all!

> I have a problem reading characters from a file.  This program is
> supposed to read characters from a text file and output to a screen.
> However, when I input:

> *****
> ** **
> *****
>  output is:
> *****
> ** **
> ***
> (star[2][3] and star[2][4] are not printed)
> When I tried to find out the problem using "watch","*" was assigned
> both arrays, but they are not printed.  I totally don't understand
> what wrong is.  Please somebody teach me how to solve the problem.

> Thanks! I appreciate any advice.

> Ryuji

Your file consist of chars:
'*','*','*','*','*','\n','*','*',' ','*','*','\n','*','*','*','*','*' (17
chars), but you read only
first 15 of them.
('\n' = next line)

The

for(row1=0;row1<3;row1++)
   {
        for(col1=0;col1<5;col1++)

          fscanf(fp,"%c",&star[row1][col1]);

        fscanf(fp,"%c", chr); /****chr earlier declared as char ***/

   }/*for*/

,  should give the good result.

Kuba

------=_NextPart_000_01BC7862.A5DA3340
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<html><head></head><BODY bgcolor=3D"#FFFFFF"><p><font size=3D2 =
color=3D"#000000" face=3D"Arial"><br><br><br><br>Ryuji &nbsp;&lt;<font =

wrote in article &lt;<font =

color=3D"#000000">&gt;...<br>&gt; Hello all!<br>&gt; <br>&gt; I have a =
problem reading characters from a file. &nbsp;This program is<br>&gt; =
supposed to read characters from a text file and output to a =
screen.<br>&gt; However, when I input:<br>&gt; <br>&gt; *****<br>&gt; ** =
**<br>&gt; *****<br>&gt; &nbsp;output is:<br>&gt; *****<br>&gt; ** =
**<br>&gt; ***<br>&gt; (star[2][3] and star[2][4] are not =
printed)<br>&gt; When I tried to find out the problem using =
&quot;watch&quot;,&quot;*&quot; was assigned<br>&gt; both arrays, but =
they are not printed. &nbsp;I totally don't understand<br>&gt; what =
wrong is. &nbsp;Please somebody teach me how to solve the =
problem.<br>&gt; <br>&gt; Thanks! I appreciate any advice.<br>&gt; =
<br>&gt; Ryuji<br><br>Your file consist of chars: =
<br>'*','*','*','*','*','\n','*','*',' =
','*','*','\n','*','*','*','*','*' (17 chars), but you read =
only<br>first 15 of them.<br>('\n' =3D next =
line)<br><br>The<br><br>for(row1=3D0;row1&lt;3;row1++)<br> =
&nbsp;&nbsp;{<br>&#009;for(col1=3D0;col1&lt;5;col1++)<br><br>&#009; =
&nbsp;fscanf(fp,&quot;%c&quot;,&amp;star[row1][col1]);<br> =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><font =
size=3D3><b>&#009;fscanf(fp,&quot;%c&quot;, chr); /****chr earlier =
declared as char ***/<br></b><font size=3D2><br> =
&nbsp;&nbsp;}/*for*/<br><br>, &nbsp;should give the good =
result.<br><br>Kuba <br><br></p>
</font></font></font></font></font></font></font></body></html>
------=_NextPart_000_01BC7862.A5DA3340--



Tue, 30 Nov 1999 03:00:00 GMT  
 Help! reading from a file and output



Quote:
> Hello all!

> I have a problem reading characters from a file.  This program is
> supposed to read characters from a text file and output to a screen.
> However, when I input:

> *****
> ** **
> *****
>  output is:
> *****
> ** **
> ***

[snip]
*****[\n]** **[\n]*****
The [\n] represents one newline character.  Count them up and you will see
why you are two short.

Quote:
> int main(void)
> {
>   FILE *fp;
>   char star[3][5];

     char star[3][6];

Quote:
>    for(col1=0;col1<5;col1++)

        for(col1=0;col1<6;col1++)
You could also store the file as:
******* *******
and put a newline after each row.

Six of one, half a dozen of the other.
All the right header files, and main returns int!  You are doing well for a
beginner.



Wed, 01 Dec 1999 03:00:00 GMT  
 Help! reading from a file and output

This is a multi-part message in MIME format.

------=_NextPart_000_01BC7869.ADF5D640
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit



Quote:
> Hello all!

> I have a problem reading characters from a file.  This program is
> supposed to read characters from a text file and output to a screen.
> However, when I input:

> *****
> ** **
> *****
>  output is:
> *****
> ** **
> ***
> (star[2][3] and star[2][4] are not printed)
> When I tried to find out the problem using "watch","*" was assigned
> both arrays, but they are not printed.  I totally don't understand
> what wrong is.  Please somebody teach me how to solve the problem.

> Thanks! I appreciate any advice.

> Ryuji

Here is the corrected version of your program:
I'm sorry, the previous Re wasn't complete....

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

int main(void)
{
  FILE *fp;
  char star[3][6],a;
  int row1,col1,row2,col2;

if((fp = fopen("star.dat","r")) == NULL)
  {
        fprintf(stderr,"Error");
        exit(1);
  }/*if*/
for(row1=0;row1<3;row1++)
   {
        for(col1=0;col1<5;col1++)

          fscanf(fp,"%c",&star[row1][col1]);
        fscanf(fp,"%c", &a);
 }/*for*/
 fclose(fp);

for(row2=0;row2<3;row2++)
{
  for(col2=0;col2<5;col2++)
        {
        printf("%c",star[row2][col2]);
        }/*for*/
  printf("\n");

Quote:
}/*for*/

return 0;

Quote:
}

Kuba

------=_NextPart_000_01BC7869.ADF5D640
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<html><head></head><BODY bgcolor=3D"#FFFFFF"><p><font size=3D2 =
color=3D"#000000" face=3D"Arial">Ryuji &nbsp;&lt;<font =

wrote in article &lt;<font =

color=3D"#000000">&gt;...<br>&gt; Hello all!<br>&gt; <br>&gt; I have a =
problem reading characters from a file. &nbsp;This program is<br>&gt; =
supposed to read characters from a text file and output to a =
screen.<br>&gt; However, when I input:<br>&gt; <br>&gt; *****<br>&gt; ** =
**<br>&gt; *****<br>&gt; &nbsp;output is:<br>&gt; *****<br>&gt; ** =
**<br>&gt; ***<br>&gt; (star[2][3] and star[2][4] are not =
printed)<br>&gt; When I tried to find out the problem using =
&quot;watch&quot;,&quot;*&quot; was assigned<br>&gt; both arrays, but =
they are not printed. &nbsp;I totally don't understand<br>&gt; what =
wrong is. &nbsp;Please somebody teach me how to solve the =
problem.<br>&gt; <br>&gt; Thanks! I appreciate any advice.<br>&gt; =
<br>&gt; Ryuji<br><br><br><font size=3D3>Here is the corrected version =
of your program:<br>I'm sorry, the previous Re wasn't =
complete....<br><font size=3D2 face=3D"Courier New"><br>#include =
&lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>int =
main(void)<br>{<br> &nbsp;FILE *fp;<br> &nbsp;char star[3][6],a;<br> =
&nbsp;int row1,col1,row2,col2;<br><br>if((fp =3D =
fopen(&quot;star.dat&quot;,&quot;r&quot;)) =3D=3D NULL)<br> =
&nbsp;{<br>&#009;fprintf(stderr,&quot;Error&quot;);<br>&#009;exit(1);<br>=
 &nbsp;}/*if*/<br>for(row1=3D0;row1&lt;3;row1++)<br> =
&nbsp;&nbsp;{<br>&#009;for(col1=3D0;col1&lt;5;col1++)<br><br>&#009; =
&nbsp;fscanf(fp,&quot;%c&quot;,&amp;star[row1][col1]);<br> =
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fscanf(fp,&quot;%c&quot;, =
&amp;a);<br> }/*for*/<br> =
fclose(fp);<br><br>for(row2=3D0;row2&lt;3;row2++)<br>{<br> =
&nbsp;for(col2=3D0;col2&lt;5;col2++)<br>&#009;{<br>&#009;printf(&quot;%c&=
quot;,star[row2][col2]);<br>&#009;}/*for*/<br> =
&nbsp;printf(&quot;\n&quot;);<br>}/*for*/<br><br>return =
0;<br>}<br><br><br><font size=3D3 face=3D"Arial">Kuba<br><font size=3D2 =
face=3D"Courier New"><br><font face=3D"Arial"><br><br><br></p>
</font></font></font></font></font></font></font></font></font></font></b=
ody></html>
------=_NextPart_000_01BC7869.ADF5D640--



Wed, 01 Dec 1999 03:00:00 GMT  
 Help! reading from a file and output

Quote:

>*****
>** **
>*****

Each line is six characters long.
'*' '*' '*' '*' '*' '\n'
You were forgetting about the newline
character . So you need to dimension
the character array [3][6] and not
[3][5].

Quote:
> output is:
>*****
>** **
>***
>#include <stdio.h>
>#include <stdlib.h>

>int main(void)
>{
>  FILE *fp;
>  char star[3][5];

                       6
Quote:
>  int row1,col1,row2,col2;

>if((fp = fopen("star.dat","r")) == NULL)
>  {
>    fprintf(stderr,"Error");
>    exit(1);
>  }/*if*/
>for(row1=0;row1<3;row1++)
>   {
>    for(col1=0;col1<5;col1++)

                                       6
Quote:

>      fscanf(fp,"%c",&star[row1][col1]);
>   }/*for*/
> fclose(fp);

>for(row2=0;row2<3;row2++)
>{
>  for(col2=0;col2<5;col2++)

                              6

Quote:
>    {
>    printf("%c",star[row2][col2]);
>    }/*for*/
>}/*for*/

>return 0;
>}

Al Bowers                                
Tampa, FL

http://www.gate.net/~abowers/index.html


Thu, 02 Dec 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. read a file than produce 2 output files(HELP)

2. read a file than produce 2 output files(HELP)

3. Reading a file & output a file

4. fgets reading a configuration file: compiles OK but output wrong

5. Read command output file

6. Read and Output RTF file

7. ANSI C Run Time Erroe to output data to the output file

8. VC 5 TRACE output to a file instead of the output window

9. MFC CDC HTML Output (ala Meta File Output)

10. Need Help with output to multiple files

11. Help: add statements to generator output files

12. Please Help: Reading & Writing from/to a Unicode File produces garbage in output file

 

 
Powered by phpBB® Forum Software