Thread: need to read a User defined file during runtime

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    need to read a User defined file during runtime

    Hi,
    basically what I have to do is write a filter program that opens a file for reading. This file is User defined during runtime.
    Anybody got any ideas on how to do this??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    char filename[FILENAME_MAX];
    FILE *fp;
    
    printf ( "File to open: " );
    fflush ( stdout );
    if ( fgets ( filename, sizeof filename, stdin ) != NULL ) {
      char *newline = strrchr ( filename, '\n' );
      if ( newline != NULL )
        *newline = '\0';
      fp = fopen ( filename, "r" );
      if ( fp == NULL )
        perror ( NULL );
      ...
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    Prelude,
    sorry but when I try to use your code the complier gives two errors

    [C++ Error] DUMPFILE.cpp(47): E2268 Call to undefined function 'strrchr'.

    [C++ Error] DUMPFILE.cpp(47): E2034 Cannot convert 'int' to 'char *'.

    for this line of code

    char *newline = strrchr ( filename, '\n' );

    any ideas??

    thanks

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    woops

    just got it to work,
    my fault forgot to add in string.h

    thanks for taking the time to help me out

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by john_newbie
    the complier gives two errors

    [C++ Error] DUMPFILE.cpp(47): E2268 Call to undefined function 'strrchr'.

    [C++ Error] DUMPFILE.cpp(47): E2034 Cannot convert 'int' to 'char *'.

    any ideas??
    Here's an idea, try compiling as C instead of C++. That'll help with other issues later on that you'll end up asking about.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    John.

    Code:
    /*
    	Simple file open/close program in C.
    
    	Error checking omitted for sanity.
    */
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
    	FILE* fptr;				/* File object...	*/
    
    
    	if (argc != 2)				/* Display usage.	*/
    	{
    		printf("Usage: %s [filename]\n", argv[0]);
    	}
    	else
    	{
    		fptr = fopen(argv[1], "r");	/* Open file...		*/
    
    		printf("%s opened for reading.\n", argv[1]);
    
    		fclose(fptr);			/* Close file...	*/
    
    		printf("%s closed.\n", argv[1]);
    	}
    	return 0;
    }
    R.I.P C89

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Error checking omitted for sanity.
    I can understand your reasoning, but it really is better to assume that the people here are not stupid. If you post bulletproof code then it sets a good example.
    My best code is written with the delete key.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Off-topic bickering snipped
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. Replies: 1
    Last Post: 12-21-2002, 04:01 AM