Welcome, Guest. Please Login
YaBB - Yet another Bulletin Board
 
  HomeHelpSearchLogin  
 
problems reading files (Read 3925 times)
gustavo
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 12
problems reading files
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
Back to top
 
 
IP Logged
 
Sergei
YaBB Administrator
*****
Offline


Datamonkeys are forever...

Posts: 1658
UCSD
Gender: male
Re: problems reading files
Reply #1 - 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
Back to top
 

Associate Professor
Division of Infectious Diseases
Division of Biomedical Informatics
School of Medicine
University of California San Diego
WWW WWW  
IP Logged
 
gustavo
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 12
Re: problems reading files
Reply #2 - Nov 29th, 2006 at 10:37am
 
well, it prints only the first number for each file
Back to top
 
 
IP Logged
 
Sergei
YaBB Administrator
*****
Offline


Datamonkeys are forever...

Posts: 1658
UCSD
Gender: male
Re: problems reading files
Reply #3 - 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");
}
 



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

Cheers,
Sergei
Back to top
 

Associate Professor
Division of Infectious Diseases
Division of Biomedical Informatics
School of Medicine
University of California San Diego
WWW WWW  
IP Logged
 
gustavo
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 12
Re: problems reading files
Reply #4 - 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
Back to top
 
 
IP Logged
 
Sergei
YaBB Administrator
*****
Offline


Datamonkeys are forever...

Posts: 1658
UCSD
Gender: male
Re: problems reading files
Reply #5 - 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);
 



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
Back to top
 

Associate Professor
Division of Infectious Diseases
Division of Biomedical Informatics
School of Medicine
University of California San Diego
WWW WWW  
IP Logged
 
gustavo
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 12
Re: problems reading files
Reply #6 - Nov 30th, 2006 at 8:25am
 
it is now working fine!
thanks.

gustavo
Back to top
 
 
IP Logged