wrong print

Discussion in 'C Programming' started by chiara, Oct 5, 2005.

  1. chiara

    chiara Guest

    Hi!

    This is a piece of a code I wrote. It was perfectly working but now it
    is giving me some problems I can not understand.
    I scan a directory (ss_data) and I count its alaments. then I print the
    name of each file in the directory before opening it.Even if the names
    of the files are stored correctly in the memory the program prints the
    letter 'E'(the files are named differently,
    e.g.traind05c1__a.1.1.1.3.fasta). The program correctly opens the files
    but it does not print the names. Anybody can help?

    Thank you in advance,

    Chiara



    #include <string.h>
    #include <stdio.h>
    #include <dir.h>
    #include <dirent.h>
    #include <stdlib.h>
    #include <alloc.h>
    #include <math.h>

    char **strings,*line;
    int n_seq=0;
    int max_line_len = 1024;
    char **Amm,**Pss;
    int** amm_comp,*y_class,*y_fold,**ss_descriptor;
    char** SS;
    int** L,**range;
    int n_files,n_el,max_length;


    void generateSeq(int max_l,char* seq,int seq_len);
    char* readline(FILE *input);
    void scandir(char *dirname);
    void analyzePss(char* pss,int ind);
    int SearchSequence(char* seq, char* in);
    int SearchRange(int* seq,int l, int low, int up);



    void scandir(char *dirname)
    {
    DIR *dir;
    FILE*fp;
    char s[MAXPATH],amm,pss;
    struct dirent *ent;
    float pC, pH,pE;
    int index,i,h,k=0,max_l,n=0;

    dirname="ss_data";

    if ((dir = opendir(dirname)) == NULL)
    {
    perror("Unable to open directory");
    exit(1);
    }


    while ((ent = readdir(dir)) != NULL) //reads the directory counting
    the number of files
    {
    if(strlen(ent->d_name)>5)
    n_files++;
    }

    ...
    rewinddir(dir);


    h=0;
    while ((ent = readdir(dir)) != NULL)//reads the name of each file in
    the directory
    {
    if(strlen(ent->d_name)>5)
    {
    printf("%d:%s\n",h,ent->d_name);

    .....
    ...
     
    chiara, Oct 5, 2005
    #1
    1. Advertising

  2. chiara

    Flash Gordon Guest

    chiara wrote:
    > Hi!
    >
    > This is a piece of a code I wrote. It was perfectly working but now it
    > is giving me some problems I can not understand.
    > I scan a directory (ss_data) and I count its alaments. then I print the
    > name of each file in the directory before opening it.Even if the names


    The C programming language does not know anything about directories, so
    you must be using non-standard libraries. We only deal with the standard
    language and libraries here. Still, I'll check your code in case there
    is anything wrong with the standard bits...

    > of the files are stored correctly in the memory the program prints the
    > letter 'E'(the files are named differently,
    > e.g.traind05c1__a.1.1.1.3.fasta). The program correctly opens the files
    > but it does not print the names. Anybody can help?
    >
    > Thank you in advance,
    >
    > Chiara
    >
    >
    >
    > #include <string.h>
    > #include <stdio.h>


    The above are standard.

    > #include <dir.h>
    > #include <dirent.h>


    The above two headers are not part of the C standard, they could be from
    some form of Unix, so comp.unix.programmer might be worth investigating,
    after checking their FAQ and a few days worth of posts.

    > #include <stdlib.h>


    Another standard header.

    > #include <alloc.h>


    A very suspicions non-standard header. Are you really sure it is
    something you need?

    > #include <math.h>
    >
    > char **strings,*line;
    > int n_seq=0;
    > int max_line_len = 1024;
    > char **Amm,**Pss;
    > int** amm_comp,*y_class,*y_fold,**ss_descriptor;


    Putting the * (or **) on the type is generally considered bad style,
    since it only applies to the first variable, not all the variables.

    > char** SS;
    > int** L,**range;
    > int n_files,n_el,max_length;
    >
    > void generateSeq(int max_l,char* seq,int seq_len);
    > char* readline(FILE *input);
    > void scandir(char *dirname);
    > void analyzePss(char* pss,int ind);
    > int SearchSequence(char* seq, char* in);
    > int SearchRange(int* seq,int l, int low, int up);
    >
    > void scandir(char *dirname)
    > {
    > DIR *dir;
    > FILE*fp;
    > char s[MAXPATH],amm,pss;
    > struct dirent *ent;
    > float pC, pH,pE;
    > int index,i,h,k=0,max_l,n=0;
    >
    > dirname="ss_data";
    >
    > if ((dir = opendir(dirname)) == NULL)


    Problems with opendir don't belong here.

    > {
    > perror("Unable to open directory");
    > exit(1);


    1 is a non-standard value for exit.

    > }
    >
    >
    > while ((ent = readdir(dir)) != NULL) //reads the directory counting
    > the number of files


    Please don't use // style comments when posting to Usenet.

    > {
    > if(strlen(ent->d_name)>5)
    > n_files++;
    > }
    >
    > ...


    Not posting a complete compilable program that exhibits your problem is
    a very silly thing to do. Since you don't know what the problem is how
    do you know that it problem is not in the code you have excluded?

    > rewinddir(dir);


    You do realise that in a multi-tasking multiuser system a new file could
    have been created by now, or one of the old file deleted, don't you?

    > h=0;
    > while ((ent = readdir(dir)) != NULL)//reads the name of each file in
    > the directory
    > {
    > if(strlen(ent->d_name)>5)
    > {
    > printf("%d:%s\n",h,ent->d_name);
    >
    > ....
    > ...


    My best guess is that there is something wrong in the code you have not
    posted or something wrong in your use of a non-standard C library. I
    suggest that if I am correct and you are using a Unix variant you try in
    comp.unix.programmer where Unix specific libraries are on topic, unlike
    here. Before posting for the first time to a group ALWAYS read the FAQ
    and at least a few days worth of posts to find out what is acceptable on
    the group. If you don't understand something in the FAQ then tell people
    which part of the FAQ you have problems with or they might assume that
    you have not read it and just tell you to go read it. Also, post a
    COMPLETE example that exhibits the problem, not something with
    quantities of code missing. To get the example down to a sensible size
    delete the code that you believe is not relevant to the problem and then
    *test* to see that the problem still exists, often this exercise itself
    will identify that the problem is NOT where you thought and might enable
    you to solve it.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Oct 6, 2005
    #2
    1. Advertising

  3. chiara

    Ian Malone Guest

    Flash Gordon wrote:
    > chiara wrote:
    >
    >> Hi!
    >>
    >> This is a piece of a code I wrote. It was perfectly working but now it
    >> is giving me some problems I can not understand.
    >> I scan a directory (ss_data) and I count its alaments. then I print the
    >> name of each file in the directory before opening it.Even if the names

    >
    >
    > The C programming language does not know anything about directories, so
    > you must be using non-standard libraries. We only deal with the standard
    > language and libraries here. Still, I'll check your code in case there
    > is anything wrong with the standard bits...
    >


    <snip>
    >
    >
    > Problems with opendir don't belong here.
    >
    >> {
    >> perror("Unable to open directory");
    >> exit(1);

    >
    >
    > 1 is a non-standard value for exit.
    >


    The OP may be interested to know that EXIT_FAILURE
    is an option here (from <stdlib.h>). EXIT_SUCCESS
    and 0 have the same meaning.

    --
    imalone
     
    Ian Malone, Oct 6, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew
    Replies:
    7
    Views:
    674
    Priscilla Walmsley
    Jan 7, 2005
  2. Gary Herron
    Replies:
    40
    Views:
    877
    garyrob
    Dec 21, 2005
  3. QQ

    Anything wrong with the print?

    QQ, Jul 12, 2006, in forum: C Programming
    Replies:
    4
    Views:
    251
    Thomas J. Gritzan
    Jul 12, 2006
  4. keto
    Replies:
    0
    Views:
    964
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    363
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page