need a bit of help with my C program please

This is a discussion on need a bit of help with my C program please within the C Programming forums, part of the General Programming Boards category; hi there! Im completely stuck at this point and any help whatsoever would be great. ive got 3 main problems. ...

  1. #1
    Unregistered
    Guest

    Unhappy need a bit of help with my C program please

    hi there!

    Im completely stuck at this point and any help whatsoever would be great.

    ive got 3 main problems.
    - determining the number of lines in a file
    - getting rid of a newline character at the end of a string
    - converting a time_t variable to a string

    heres the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <string.h>
    #include <time.h>
    
    typedef struct
    {
    	char name[256];
    	u_short mode;
    	short user_id;
    	short group_id;
    	off_t size;
    	char time_last_mod[50];
    } file_sig;
    
    main(int argc, char **argv)
    {
    	struct stat file_info;
    	file_sig files[3]; //** need to make the files array = to the number of filenames, that is the number of lines in the input file **//	
    	int i=0;
    
    	FILE *fptr;
    	char filename[200];
    	fptr = fopen(argv[1], "r");
    	fgets(filename, 200, fptr); //** need to get rid of the newline character at the end of filename **//
    
     	while (!feof(fptr))
    	{	
    		stat(filename, &file_info); //** doesnt work cos of the newline character **//
    		time (&file_info.st_mtime);
    
    		files[i].name=filename; //** doesnt work cos they are incompatible data types due to newline character **//
    		files[i].mode=file_info.st_mode;
    		files[i].user_id=file_info.st_uid;
    		files[i].group_id=file_info.st_gid;
    		files[i].size=file_info.st_size;
    		files[i].time_last_mod=ctime(&file_info.st_mtime); //** doesnt work because they are incompatible data types **//
    
    	        printf("\n\nFilename: %s",files[i].name); //** doesnt work due to previous errors **//
           	        printf("Protection and Filetype: %o\n", files[i].mode);
                    printf("User ID of owner: %o\n", files[i].user_id);
           	        printf("Group ID of owner: %o\n", files[i].group_id);
           	 	printf("File Size in bytes: %o\n", files[i].size);
           	 	printf("Last modify time: %s\n", files[i].time_last_mod); //** doesnt work due to previous errors *//
    
    		++i;			
    
    		fgets(filename, 200, fptr); //** need to get rid of the newline character at the end of filename again **//
    	}
    	fclose(fptr);
    }
    any help would be great

    -Sandy

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    for converting time_t to string couldnt you do this:
    char str;
    time_t mytime;
    mytime = time(NULL);
    strcpy(str,mytime);

    not sure if it will work but it should.

    -Luke
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Lynux-Penguin
    for converting time_t to string couldnt you do this:
    char str;
    time_t mytime;
    mytime = time(NULL);
    strcpy(str,mytime);

    not sure if it will work but it should.

    -Luke
    How on earth would this work?

    1) 'str' is just a single character.
    2) 'time_t' is basicly a long integer.
    3) strcpy( str, mytime ) just copies the time value into a single character.

    [edit] Except in this case, it would use the value in 'mytime' as a pointer, and really screw you over. Actually you'd get some severe warnings when you compiled here. [/edit]

    4) This still doesn't give you the current time. It gives you a huge number, assuming it worked.

    Quzah.
    Last edited by quzah; 05-15-2002 at 06:43 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    Unregistered
    Guest

    Talking errrmm....

    thanks for the help but it didnt really help :/

    st_mtime is supposed to give the modify time of the file, which is this long messed up integer... im trying to make that into a readable string...

    thanks anyway tho

    -Sandy

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >file_sig files[3];
    >determining the number of lines in a file
    Your program only performs a stat() then prints the data. In this case there's no need to have an array of file_sig structs, one will do. Just reuse it in each iteration of the loop.
    However, if you are going to expand this prog, post again and I'll help you get round this one.

    >files[i].name=filename; //** doesn't work cos they are incompatible data types due to newline character **//
    No. It doesn't work because you can't copy strings from one array to another in this manner. Use strncpy().

    >getting rid of a newline character at the end of a string
    Easy, just blat over it with a '\0'. Something like
    >filename[strlen(filename)-1] = '\0';

    >converting a time_t variable to a string
    ctime() does that for you, but it returns a pointer to a static variable held within the ctime function (I believe). If you want to store your own copy, just strcpy into another variable. As in:
    Code:
    time_t time_of_day;
    char buf[30];
    time_of_day = time( NULL );
    strncpy (buf, ctime( &time_of_day), 30);
    printf( "It is now: %s" ,  buf);
    Be warned, the ctime() function leaves a trailing newline character in the string, if you don't want it, just use the same code to remove as per the filename example.

    Other observations:
    >while (!feof(fptr))
    try to use the return code from fgets() to determine when the end of a file is reached.

    >fptr = fopen(argv[1], "r");
    Always ensure the fptr is not NULL before using it You don't do any validation, so your prog will crash and burn if you don't have access or the file is missing. (probably core)

    >stat(filename, &file_info)
    Again, check the return code from stat to ensure the call worked before using the values in the file_info struct. If the call failed, I don't believe the contents of file_info are guaranteed.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21