Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

File reading and no output.

chgaychgay Member Posts: 4
I have written a Prog in C code running on Unix to read wtmpx.txt and the prog will output name and duration. wtmpx.txt is a logfile to keep track of users logging into the network. In the programme, I have to output userName and duration.

The lines of wtmpx.txt is as such like this:
david pts/2 130.130.64.3 Thu Jan 16 11:58 - 12:01 (00:02)
cdf pts/2 130.130.64.5 Thu Jan 16 22:40 still logged in

I break the lines into 8 fields as declared as strings in the prog.

Compilation is successful, however, there is no output and no prompt $. The file exists in my home directory and the contents are there. Really puzzled.

Also, can anyone suggest something on handling "still logged in". That line will be ignored and read the next line that have duration in the format (hh:mm).
I have:
if (dash != "-")
{

}
Your advice and comments are greatly appreciated.

- Horace

#include
#include

int main()
{
char name[700][8]; /*700 indicates the number of lines in the file*/
char term[700][8];
char host[700][17];
char date[700][11];
char start[700][6];
char dash[700][2];
char end[700][6];
char mins[700][7];
int i=0;
int c=0;

const char FILENAME[] = "wtmpx.txt";
FILE* ins;
ins = fopen(FILENAME, "r");

if (ins != NULL)
{
while(!feof(ins) && (i < 700));
{
fscanf(ins, "%s %s %s %s %s %s %s %s", name[i], term[i], host[i], date[i], start[i], dash[i], end[i], mins[i]);

i++;

if (dash != "-")
{
/* Read next line if "till logged in" */
}
}

while(c<i);
{
printf("
%s %s
", name, mins);
c++;
}
fclose(ins);
}
else
{
printf("
Could not open file wtmpx.txt
");
}
return 0;
}

Comments

  • zeronezerone Member Posts: 24
    [b][red]This message was edited by Moderator at 2003-5-6 8:27:55[/red][/b][hr]
    Don't use a ";" after a while loop and the "&&" must be "||".

    while(!feof(ins) && (i < 700)); => while(!feof(ins) || (i < 700))


    cheers,
    Z


    : I have written a Prog in C code running on Unix to read wtmpx.txt and the prog will output name and duration. wtmpx.txt is a logfile to keep track of users logging into the network. In the programme, I have to output userName and duration.
    :
    : The lines of wtmpx.txt is as such like this:
    : david pts/2 130.130.64.3 Thu Jan 16 11:58 - 12:01 (00:02)
    : cdf pts/2 130.130.64.5 Thu Jan 16 22:40 still logged in
    :
    : I break the lines into 8 fields as declared as strings in the prog.
    :
    : Compilation is successful, however, there is no output and no prompt $. The file exists in my home directory and the contents are there. Really puzzled.
    :
    : Also, can anyone suggest something on handling "still logged in". That line will be ignored and read the next line that have duration in the format (hh:mm).
    : I have:
    : if (dash != "-")
    : {
    :
    : }
    : Your advice and comments are greatly appreciated.
    :
    : - Horace
    :
    : #include
    : #include
    :
    : int main()
    : {
    : char name[700][8]; /*700 indicates the number of lines in the file*/
    : char term[700][8];
    : char host[700][17];
    : char date[700][11];
    : char start[700][6];
    : char dash[700][2];
    : char end[700][6];
    : char mins[700][7];
    : int i=0;
    : int c=0;
    :
    : const char FILENAME[] = "wtmpx.txt";
    : FILE* ins;
    : ins = fopen(FILENAME, "r");
    :
    : if (ins != NULL)
    : {
    : while(!feof(ins) && (i < 700));
    : {
    : fscanf(ins, "%s %s %s %s %s %s %s %s", name[i], term[i], host[i], date[i], start[i], dash[i], end[i], mins[i]);
    :
    : i++;
    :
    : if (dash != "-")
    : {
    : /* Read next line if "till logged in" */
    : }
    : }
    :
    : while(c<i);
    : {
    : printf("
    %s %s
    ", name, mins);
    : c++;
    : }
    : fclose(ins);
    : }
    : else
    : {
    : printf("
    Could not open file wtmpx.txt
    ");
    : }
    : return 0;
    : }
    :
    :

    [italic]"We must work in team, or we die"[/italic]
    - The single dead guy WWII.



  • c_mallic_malli Member Posts: 10
    Hi,
    The first thing is the semicolon (;) after while statement.

    Secondly we can't compare char array with the constant sting.
    ie,
    if (dash != "-")
    instead of u compare using strcmp
    if(strcmp(dash, "-") == 0) {
    }

    Rgds,
    Malli

    : I have written a Prog in C code running on Unix to read wtmpx.txt and the prog will output name and duration. wtmpx.txt is a logfile to keep track of users logging into the network. In the programme, I have to output userName and duration.
    :
    : The lines of wtmpx.txt is as such like this:
    : david pts/2 130.130.64.3 Thu Jan 16 11:58 - 12:01 (00:02)
    : cdf pts/2 130.130.64.5 Thu Jan 16 22:40 still logged in
    :
    : I break the lines into 8 fields as declared as strings in the prog.
    :
    : Compilation is successful, however, there is no output and no prompt $. The file exists in my home directory and the contents are there. Really puzzled.
    :
    : Also, can anyone suggest something on handling "still logged in". That line will be ignored and read the next line that have duration in the format (hh:mm).
    : I have:
    : if (dash != "-")
    : {
    :
    : }
    : Your advice and comments are greatly appreciated.
    :
    : - Horace
    :
    : #include
    : #include
    :
    : int main()
    : {
    : char name[700][8]; /*700 indicates the number of lines in the file*/
    : char term[700][8];
    : char host[700][17];
    : char date[700][11];
    : char start[700][6];
    : char dash[700][2];
    : char end[700][6];
    : char mins[700][7];
    : int i=0;
    : int c=0;
    :
    : const char FILENAME[] = "wtmpx.txt";
    : FILE* ins;
    : ins = fopen(FILENAME, "r");
    :
    : if (ins != NULL)
    : {
    : while(!feof(ins) && (i < 700));
    : {
    : fscanf(ins, "%s %s %s %s %s %s %s %s", name[i], term[i], host[i], date[i], start[i], dash[i], end[i], mins[i]);
    :
    : i++;
    :
    : if (dash != "-")
    : {
    : /* Read next line if "till logged in" */
    : }
    : }
    :
    : while(c<i);
    : {
    : printf("
    %s %s
    ", name, mins);
    : c++;
    : }
    : fclose(ins);
    : }
    : else
    : {
    : printf("
    Could not open file wtmpx.txt
    ");
    : }
    : return 0;
    : }
    :
    :

Sign In or Register to comment.