Thread: Opening a TIFF file

  1. #1
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49

    Opening a TIFF file

    Gudday all
    I am trying to move TIFF files on the same server from one location to another. I have tried many things but my latest attempt is to open the file, read it in and write that same file to the new location.
    I thought it would be easy but I keep getting unable to open file errors.
    The copy function is
    Code:
    void copy(char *srcFilename, char *dstFileName)
    {
    FILE *fsrc, *fdst;
    char c;
    
    printf("Inside copy function\n");
    fsrc=fopen(srcFilename, "rb");
    if (fsrc==NULL) {
    	printf("Couldn't open file %s for reading.\n", srcFilename);
        exit(0);
    }
     /* Add usual error check */
    fdst=fopen (dstFileName, "w");
    /*Add usual error check */
    for ( ; !feof(fsrc); )
    fputc (fgetc (fsrc), fdst);
    fclose (fsrc);
    fclose (fdst);
    }
    and I call it using
    Code:
    copy(tiff_file, tiffLine);
    where tiffline is the destination and has the form "c:\TestingTiffMoves\AU\PICKLIST\0000544B.TIF" .
    The path is fixed but the file name will be variable in name but not in length.
    tiff_file is the local file, in this case "0000544B.TIF"

    The error message is "Couldn't open file 0000544B.TIF for reading."

    Is there a trick to this that I am missing?

    Also how to we express paths in C? I am giivng the absolute path at present. Is it possible to use relative paths? If so what is the format?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The backslashes needs to be doubled, since one backslash is an escape char in C.

    "\\test.txt", instead of "\test.txt" to open the test.txt file on the drive's root directory.

    Relative paths are supported.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Adak View Post
    The backslashes needs to be doubled, since one backslash is an escape char in C.

    "\\test.txt", instead of "\test.txt" to open the test.txt file on the drive's root directory.

    Relative paths are supported.
    Backslashes only need to be escaped inside string literals. Presumably, pathnames are not hard-coded into the program itself so this is not an issue.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    The path is coming from a file created previously.
    The contents of the file (each line is tiffline) looks like
    Code:
    c:\TestingTiffMoves\AU\PICKLIST\0000544B.TIF
    c:\TestingTiffMoves\AU\PICKLIST\0000544C.TIF
    c:\TestingTiffMoves\AU\PICKLIST\0000544D.TIF
    c:\TestingTiffMoves\AU\PICKLIST\0000544E.TIF
    The c: is not a part of the code that creates the file contents. I added that to see if it needed the full path to be able to open the file for writing but it is not getting that far anyway.

    Is
    Code:
    fsrc=fopen(srcFilename, "rb");
    sufficient to open the TIFF file or do I need more?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, that is the proper format for opening a file in binary mode.

    You can forget the TIFF part - that makes no difference - a file is a file. Some files or directories may require you to have a certain administrative privilege in order to open it, but that is a function of the OS, not C.

    When you step through your code, where is it going off the rails?
    Last edited by Adak; 08-06-2009 at 11:13 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Are you reading the list of filenames using fgets()?
    Are you stripping off the \n before trying to use it as a filename in an fopen() call?
    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.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you make a tiny test.txt file, you can see this program at work

    Code:
    //snippet to show opening of a file
    
    #include <stdio.h>
    
    int main(void)  {
      FILE *in;
      int ch;
      char fname[20];  
      putchar('\n');
    
      //either of these string literals work OK.
    //  in = fopen("C:\\tc\\bin\\test.txt", "rt");
      in = fopen("C:/tc/bin/test.txt", "rt");
    
      //curly braces are not required here, since the while statement   
      //is only one statement on two lines. Helps for clarity, however.
      if(in) { 
         while((ch=fgetc(in)) != EOF)
            printf("%c", ch);
      }
      else  {
        printf("\n Error opening file test.txt. Program terminated.\n");
        return 1;
      }
      
      fclose(in);
    
      //end of filename as a string literal
      //start building up a char array into a filename
      putchar('\n');
      fname[0] = 't';
      fname[1] = 'e';
      fname[2] = 's';
      fname[3] = 't';
      fname[4] = '.';
      fname[5] = 't';
      fname[6] = 'x';
      fname[7] = 't';
      fname[8] = '\0';
    
      in = fopen(fname, "rt");
      if(in)                //showing the curly braces aren't necessary
         while((ch=fgetc(in)) != EOF)
            printf("%c", ch);
      else  {
        printf("\n Error opening file test.txt for the second time. Program terminated.\n");
        return 1;
      }
      ch = getchar();
      fclose(in);
      return 0;
    }

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Quote Originally Posted by jam00 View Post
    A +ve number a is a proper factor of X if X is a multiple of a and a does not equal 1 or X. The user gives a int[] factors containing all the proper factors of int X.
    This program should return X to the user.
    Could someone help me with the algoirthim of this problem it is like getting the all the possible factors of a number but the oppisite way.

    thanx! [:
    Don't try to hijack threads, don't break forum rules.
    http://cboard.cprogramming.com/c-pro...e-posting.html
    http://cboard.cprogramming.com/c-pro...-homework.html

  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
    jam00 posts have been deleted. @jam00, post new threads with EFFORT.
    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.

  10. #10
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Adak
    I complied your program and ran it but it nevers gets to the 'error opening for the 2nd time' as it fails the 1st run everytime. The program cannot find "C:/tc/bin/test.txt", "rt" as it never gets created.
    Perhaps my ignorance is appalling?

    If I were to make filename[len] = 0 would that make sure the any newlines/carriage returns on the end of the fle name were removed or rather neutralised?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Tigers! View Post
    Adak
    I complied your program and ran it but it nevers gets to the 'error opening for the 2nd time' as it fails the 1st run everytime. The program cannot find "C:/tc/bin/test.txt", "rt" as it never gets created.
    Perhaps my ignorance is appalling?

    If I were to make filename[len] = 0 would that make sure the any newlines/carriage returns on the end of the fle name were removed or rather neutralised?
    I thought I made it clear that you needed to create a tiny text file named "test.txt", before you ran the program.

    It was designed to show YOU how to open a file that already existed, (like your TIFF files, but in text mode for this example).

    The end of string char is '\0', not zero. Use that. Yes, it will stop any string function from including char's after the '\0', as long as you don't reference a char or string directly, that lies *past* the '\0'. Which is confusing, but this is an example:

    I have a string[] of char. I put an end of string marker char, into string[0]. But then I reference char's which are still in string[], and lie beyond the end of string char, directly:

    Code:
    /* note the space before the 'I'. Not being tricky, just neat. It works the same no matter what the
        first char is.
    */
    
    char string[] = " I am still here";
    
    string[0] = '\0';
    
    printf("\n String is: %s now stopped", string);
    
    printf("\n String is referenced directly after the end of string char:\n");
    printf("\n String is: %s now printing", string+1);
    If your code might reference a spot on the string array beyond the end of string char, then use a for loop, from 0 to sizeof(string), and set every string index to the value of the end of string char.

    That's rarely needed, however.
    Last edited by Adak; 08-09-2009 at 10:35 PM.

  12. #12
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Yes my ignorance is appalling.

    I have now created test.txt (with content "Does this work ok?") and it does the 1st run, printing out the content, and fails at the 2nd.

    Looking at your code I think it failed the 2nd run because the path was wrong. Test.txt is not in the same directory as the executable. When I put test.txt in the same directory as the executable it printed out the contents twice.

    I'll try stripping off the '\n' and replace with '\0'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM