#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    C - Copying file and flipping


    Just starting some basic C for a module and while I had a brief affair with C++ years, most of my coding over the past 6 months has been Java in the Eclipse environment.

    For this, I'm using gcc and gedit.

    I am trying to copy content from a file, and reverse the characters.

    Eg.

    File contains

    abcdefghijklmnopqrstuvwxyz;
    ABCDEFGHIJKLMNOPQRSTUVWXYZ,

    should display

    zyx etc....;
    ZYX etc..,

    My code is as follows

    copyFile.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "utils.h"
    
    int main(int argc, char* argv[])
    {
      char c;
      FILE *from, *to;
    
      int flipping = (strcmp(argv[1], "-f") == 0);
    
      from = fopen(argv[2], "r");
      to = fopen(argv[3], "w");
    
      if (from == NULL)
      {
        perror(argv[2]);
        exit(1);
      }
    
      if (to == NULL)
      {
    
    	to = fopen(argv[3], "w+");
      }
    
      /* file exists, so start reading */
    	if (flipping)
    	{  
    	  putc(flipChar(c), to);
    	}
    
    	else
    	{
    	  putc((c), to);
    	}
    	
    	
    
      fclose(from);
      fclose(to);
    
      exit(0);
    }
    My Utils.c file is

    Code:
    #include "utils.h"
    
    char flipChar(char c)
    {
    if ('a' <= c && c <= 'z')
    { 
    	return c-'a' + 'A'; 
    }
    
    else if ('A' <= c && c <= 'Z')
    {
    
    	return c-'a' + 'A';
    }
    
    else if (0 <= c && c <= 9)
    {
    
    	return (c + 1);
    }
    
    return c;
    
    }
    My utils.h file is
    Code:
    #ifndef UTILS_H
    #define UTILS_H
    
    char flipChar();
    
    #endif
    I am using the following commands

    gcc -c -o copyFile.o copyFile.c
    gcc -c -o utils.o utils.c

    gcc copyFile.o utils.o -o copyFile

    However, at this point, I get an error

    utils.o: file not recognized: File format not recognized
    collect2: error: ld returned 1 exit status


    It's probably simple to ye but I'm out of my language and IDE environment!!! :)
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Why not just do it all in one fell swoop (turning on warnings as well)?

    Code:
    gcc -Wall -pedantic -o copyFile copyFile.c utils.c
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Are you sure this is the intended way to 'flip' the character? Are you not to reverse the _order_ of the string?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by mitakeet
    Are you sure this is the intended way to 'flip' the character? Are you not to reverse the _order_ of the string?
    I'm sure I could have gotten it wrong, but honestly, the function role itself isn't that important, it's more being able to pass variables between functions and using -f to go between flipping (but really just calling the function or not).

    I complied but got this


    copyFile.c: In function 'main':
    copyFile.c:31:4: warning: implicit declaration of function 'flipChar' [-Wimplicit-function-declaration]
    copyFile.c:31:4: warning: 'c' may be used uninitialized in this function [-Wuninitialized]
    utils.c:3:6: error: conflicting types for 'flipChar'
    utils.c:4:1: note: an argument type that has a default promotion can't match an empty parameter name list declaration
    In file included from utils.c:1:0:
    utils.h:4:6: note: previous declaration of 'flipChar' was here
  8. #5
  9. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    I didn't look at your code, just going by the warning message...

    You must declare a function before you use it (the function body appearing before use gets you the same thing). Perhaps you failed to include the appropriate header?

    'c' may be uninitialized can result in serious bugs that are very difficult to resolve. That is an important warning and trivial to remedy (when you declare it, set it to a default value), but more important at this point is you are probably using the variable (as an 'r-value') before you have done something useful with it (assigning a value to it as an 'l-value').

    'conflicting types' means you declared the function to take, say, a char and are passing it, for instance, an unsigned char (or int, float, etc.).

    'argument type' means there is no automatic promotion path available (char to int, for instance (you generally don't get automatic promotion of signed/unsigned types in C++ since that can cause subtle problems, but is often allowed in C)).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by chrislad
    I'm sure I could have gotten it wrong, but honestly, the function role itself isn't that important, it's more being able to pass variables between functions and using -f to go between flipping (but really just calling the function or not).

    I complied but got this


    copyFile.c: In function 'main':
    copyFile.c:31:4: warning: implicit declaration of function 'flipChar' [-Wimplicit-function-declaration]
    copyFile.c:31:4: warning: 'c' may be used uninitialized in this function [-Wuninitialized]
    utils.c:3:6: error: conflicting types for 'flipChar'
    utils.c:4:1: note: an argument type that has a default promotion can't match an empty parameter name list declaration
    In file included from utils.c:1:0:
    utils.h:4:6: note: previous declaration of 'flipChar' was here
    Managed to get it working! Thanks! Had an extra #include
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0
    And on that note, here's the 'flipping' code I used.

    Code:
    
    char flipChar(char c)
    {
    if ('a' <= c && c <= 'z')
    { 
    	return 'z'- c + 'A'; 
    }
    
    else if ('A' <= c && c <= 'Z')
    {
    
    	return 'Z'- c + 'a';
    }
    
    else if ('0' <= c && c <= '9')
    {
    	return '9' - c + '0';
    }
    
    return c;
    
    }
    Thanks for the pointers, guys!

IMN logo majestic logo threadwatch logo seochat tools logo