HyPhy message board
http://www.hyphy.org/cgi-bin/hyphy_forums/YaBB.pl
HYPHY Package >> HyPhy feedback >> problems reading files
http://www.hyphy.org/cgi-bin/hyphy_forums/YaBB.pl?num=1164824073

Message started by gustavo on Nov 29th, 2006 at 10:14am

Title: problems reading files
Post by gustavo on Nov 29th, 2006 at 10:14am
Hi,
I have some problems reading two files at the same time using the following part of a script.

fileName = "1bm8-str";
fileName1 = "1l8r-str";

 
  for(k=0;k<72;k=k+1){
       fscanf (fileName,"Number", i);
      fprintf (stdout," ",i);
       fscanf (fileName1,"Number", j);
       fprintf (stdout," ",j);

  }

each file contains a column of integers.
thank you for your help

gustavo parisi

Title: Re: problems reading files
Post by Sergei on Nov 29th, 2006 at 10:27am
Dear Gustavo,

The script looks alright, expect the numbers won't be printed to the console until the new line terminator is found or the output buffer is full.

What exactly is going wrong with the execution?

Cheers,
Sergei

Title: Re: problems reading files
Post by gustavo on Nov 29th, 2006 at 10:37am
well, it prints only the first number for each file

Title: Re: problems reading files
Post by Sergei on Nov 29th, 2006 at 11:07am
Dear Gustavo,

Try

[code]
for(k=0;k<72;k=k+1){  
  fscanf (fileName,"Number", i);  
  fprintf (stdout,i, "\n");  
  fscanf (fileName1,"Number", j);
  fprintf (stdout,j, "\n");  
}
[/code]

If that doesn't help, you may have to send me the input files so I can check what's going on.

Cheers,
Sergei

Title: Re: problems reading files
Post by gustavo on Nov 30th, 2006 at 1:50am
dear sergei,
I'm sorry but I can't find the error!
For example using two files named "first" and "second"

"first" contains in one column

1
2
3
4
5

"second" file contains in one column

6
7
8
9
10

Using the code you sent

fileName = "first";
fileName1 = "second";

 
for(k=0;k<5;k=k+1){
  fscanf (fileName,"Number", i);
  fprintf (stdout,i, "\n");
  fscanf (fileName1,"Number", j);
  fprintf (stdout,j, "\n");

I obtain

1
6
1
6
1
6
1
6
1
6

if I use

fileName = "first";
fileName1 = "second";

for(k=0;k<5;k=k+1){
  /*fscanf (fileName,"Number", i);
  fprintf (stdout,i, "\n");*/
  fscanf (fileName1,"Number", j);
  fprintf (stdout,j, "\n");

the output is
6
7
8
9
10

and for

fileName = "first";
fileName1 = "second";

for(k=0;k<5;k=k+1){
  fscanf (fileName,"Number", i);
  fprintf (stdout,i, "\n");
  /*fscanf (fileName1,"Number", j);
  fprintf (stdout,j, "\n");*/

1
2
3
4
5

thank you very much for your help! best regards,
gustavo

Title: Re: problems reading files
Post by Sergei on Nov 30th, 2006 at 8:02am
Dear Gustavo,

Sorry I should have realized what was going on yesterday.
HyPhy actually can't properly read data from two files at once. It's definitely a limitation, but I hope not a major one. I  should actually fix the issue (because fprintf to multiple files at once works well).

When you call fscanf, HyPhy does the following

1). Open the file
2). See if this file has been read from with the last call of fscanf
3). If (2) is true, advances the file pointer to a correct location, if not, leave the pointer at the beginning of the file
4). Reads from the file
5). Set END_OF_FILE if the end of the file has been reached.

Hence when you call fscanf on alternating files, (3) will always read from the beginning of the file.

Sorry for the confusion.

You can fix the problem with the code by writing two loops, or by calling


Code (]
fscanf (file1,"Lines",lines1);
fscanf (file2,"Lines",lines2);
[/code):



This will read all lines in the file into column vectors, and then you can say

[code]
for(k=0;k<72;k=k+1){  
 i = 0+lines1[k]; /* force string to number conversion */
 fprintf (stdout," ",i);  
  j = 0 + lines2[k];
  fprintf (stdout," ",j);
 
  }


Cheers,
Sergei

Title: Re: problems reading files
Post by gustavo on Nov 30th, 2006 at 8:25am
it is now working fine!
thanks.

gustavo

HyPhy message board » Powered by YaBB 2.5.2!
YaBB Forum Software © 2000-2024. All Rights Reserved.