Different outputs on different OS

This is a discussion on Different outputs on different OS within the C Programming forums, part of the General Programming Boards category; Hello all. this is a great site for programming and thanks for all people who help. so am writing a ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    19

    Unhappy Different outputs on different OS

    Hello all. this is a great site for programming and thanks for all people who help. so am writing a C program in linux using gcc and gedit. the program is not yet completed. and i am short of time. the program output varies from OS to OS. the main role of the program is to encrypt a file after the user has input a text and saved it to a file. the text that is saved to the file contains no text when run in linux. when run in windows xp i get the text followed by a square. in windows 7 i get a clean text file. i wonder if someone can point me to the right direction. heres the code. am still a beginner. thanks in advance. You can go directly to the function newFile() which is the part that writes to a file

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define TEXTSIZE 256
    
    //Function Prototypes//
    
    void optionLists();
    void encryption();
    void decryption();
    void encryptionLists();
    void newFile();
    
    //Global Variables//
    
    char *line = {"\n-----------------------\n"};
    char *newline = "\n";
    short int choice;
    
    
    int main()
    {
        optionLists();
        printf(" Enter Choice: ");
        scanf( "%d", &choice);
    
        switch (choice)
        {
            case 0:
            {
                return 0;
                break;
            }
    
            case 1:
            {
                encryption();
                break;
            }
    
            case 2:
            {
                decryption();
                break;
            }
    
            default:
            {
                printf(line);
                printf(" Wrong Choice!\n");
            }
        }
    
    
    }
    
    void optionLists()
    {
        printf(" [1] Encryption\n");
        printf(newline);
        printf(" [2] Decryption\n");
        printf(newline);
        printf(" [0] Exit\n");
        printf(line);
    }
    
    
    void encryption()
    {
        printf(" E N C R Y P T I O N   M E N U\n");
        encryptionLists();
        getchar();
    }
    
    
    void decryption()
    {
        printf(" \nD E C R Y P T I O N   M E N U\n");
        getchar();
    
    }
    
    void encryptionLists()
    {
        printf(newline);
        printf(" [1] Existing File\n");
        printf(newline);
        printf(" [2] New File\n");
        printf(line);
        printf(" Enter Choice: ");
        scanf( "%d", &choice);
    
        switch (choice)
        {
            case 1:
            {
                printf("Test");
                break;
    
            }
    
            case 2:
            {
                newFile();
                break;
    
            }
    
        }
    
    
        getchar();
    }
    
    
    void newFile()
    {
        FILE *fp;
        char text[TEXTSIZE];
        int i, wordCount;
        printf(newline);
        printf(" N E W   F I L E\n");
        char filename[20];
        //char fileName[25];
        //char ext_1 = '.';
        //char ext_2 = 't';
        //char ext_3 = 'x';
        //char ext_4 = 't';
        //char ext[5] = "";
    
        //sprintf(ext, "%c%c%c%c",ext_1,ext_2,ext_3,ext_4);
    
    
        printf(line);
        printf(newline);
        printf(" Enter the name of the file: ");
        scanf("%s", &filename);
        //strcat(fileName, filename);
        //strcat(fileName, ext);
        fp = fopen(filename, "ab+");
    
        if (fp == NULL)
        {
           printf(" New file failed");
           getchar();
        }
    
        else
        {
            printf(" Enter your text below:\n");
    
            for (i = 0; i < TEXTSIZE; i++)
            {
                scanf("%c", &text[i]);
                printf("%s\n", text);
    
            }
    
    
            fp = fopen(filename, "ab+");
    
            if (fp != NULL)
            {
    
                fflush(stdin);
                fgets(text, TEXTSIZE, stdin);
                fputs(text,fp);
                fclose(fp);
            }
    
            else
            {
                printf("\nCould not create file!\n");
            }
    
    
            getchar();
    
        }
    
    
    }
    Last edited by darksifer; 10-19-2010 at 11:20 AM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Compile with warnings on and fix those first :
    In function 'main':
    27: warning: format '%d' expects type 'int *', but argument 2 has type 'short int *'
    In function 'encryptionLists':
    93: warning: format '%d' expects type 'int *', but argument 2 has type 'short int *'
    In function 'newFile':
    139: warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[20]'
    122: warning: unused variable 'wordCount'

    fflush(stdin) is undefined. Search to see why and what to replace it with.

    You're opening filename twice.

    The specific problem you're seeing probably has to do with how various OSs treat end of line characters. Since you're working with text, open your files without the "b" option.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Darksifer!

    So your output to the file has gone "dark", eh?

    Let's see here...hmmmmm.

    Ninja posted, I have been! That was quick, KC!

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    printf(line); // strongly discourage in case line contains % character
    scanf("%s", &filename); // filename is array of char. & is not needed.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    In the following code:
    Code:
        
        scanf("%s", &filename);
        //strcat(fileName, filename);
        //strcat(fileName, ext);
        fp = fopen(filename, "ab+");
    the scanf should be:
    Code:
    scanf("%s"),filename);
    Note the missing ampersand.

    Also why are you opening the file in binary mode?

    In the following code:
    Code:
        if (fp == NULL)
        {
           printf(" New file failed");
           getchar();
        }
    
        else
        {
            printf(" Enter your text below:\n");
    
            for (i = 0; i < TEXTSIZE; i++)
            {
                scanf("%c", &text[i]);
                printf("%s\n", text);
    
            }
    
    
            fp = fopen(filename, "ab+");
    
            if (fp != NULL)
            {
    
                fflush(stdin);
                fgets(text, TEXTSIZE, stdin);
                fputs(text,fp);
                fclose(fp);
            }
    At this point the file is already open. Why are you re-opening it?

    Never
    Code:
    fflush(stdin);

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    scanf("%s"),filename);

    erm..? you meant scanf("%s",filename);

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    19
    lol. am stil learnin friends. i know there are many mistakes. nut i begin to learn C yesterD. i corrected the twice opening of file. i corrected what jimblumberg stated. i removed the ampersand. the program compiled but does not wen i add ')' to the scanf. i removed th 'b' binary mode.


    @KCfromNC

    how can i make the program to enter text in the file in linux. i dnt like to use windows.

    and thanks again for such quick reply.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    19
    am behind schedule i got other assignments in other modules.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    How about re-posting your modified code.

    Jim

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    19
    @jimblumberg heres the code

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define TEXTSIZE 256
    
    //Function Prototypes//
    
    void optionLists();
    void encryption();
    void decryption();
    void encryptionLists();
    void newFile();
    
    //Global Variables//
    
    char *line = {"\n-----------------------\n"};
    char *newline = "\n";
    short int choice;
    
    
    int main()
    {
        optionLists();
        printf(" Enter Choice: ");
        scanf( "%d", &choice);
    
        switch (choice)
        {
            case 0:
            {
                return 0;
                break;
            }
    
            case 1:
            {
                encryption();
                break;
            }
    
            case 2:
            {
                decryption();
                break;
            }
    
            default:
            {
                printf(line);
                printf(" Wrong Choice!\n");
            }
        }
    
    
    }
    
    void optionLists()
    {
        printf(" [1] Encryption\n");
        printf(newline);
        printf(" [2] Decryption\n");
        printf(newline);
        printf(" [0] Exit\n");
        printf(line);
    }
    
    
    void encryption()
    {
        printf(" E N C R Y P T I O N   M E N U\n");
        encryptionLists();
        getchar();
    }
    
    
    void decryption()
    {
        printf(" \nD E C R Y P T I O N   M E N U\n");
        getchar();
    
    }
    
    void encryptionLists()
    {
        printf(newline);
        printf(" [1] Existing File\n");
        printf(newline);
        printf(" [2] New File\n");
        printf(line);
        printf(" Enter Choice: ");
        scanf( "%d", &choice);
    
        switch (choice)
        {
            case 1:
            {
                printf("Test");
                break;
    
            }
    
            case 2:
            {
                newFile();
                break;
    
            }
    
        }
    
    
        getchar();
    }
    
    
    void newFile()
    {
        FILE *fp;
        char text[TEXTSIZE];
        //int i;
        //int wordCount;
        printf(newline);
        printf(" N E W   F I L E\n");
        char filename[20];
        //char fileName[25];
        //char ext_1 = '.';
        //char ext_2 = 't';
        //char ext_3 = 'x';
        //char ext_4 = 't';
        //char ext[5] = "";
    
        //sprintf(ext, "%c%c%c%c",ext_1,ext_2,ext_3,ext_4);
    
    
        printf(line);
        printf(newline);
        printf(" Enter the name of the file: ");
        scanf("%s", filename);
        //strcat(fileName, filename);
        //strcat(fileName, ext);
        //fp = fopen(filename, "a");
    
        if (fp == NULL)
        {
           printf(" New file failed");
           getchar();
        }
    
        else
        {
            printf(" Enter your text below:\n");
    
            //for (i = 0; i < TEXTSIZE; i++)
            //{
                //scanf("%c", &text[i]);
                //printf("%s\n", text);
    
            //}
    
    
            fp = fopen(filename, "a");
    
            if (fp != NULL)
            {
    
                //fflush(stdin);//
                fgets(text, TEXTSIZE, stdin);
                fputs(text,fp);
                fclose(fp);
                return 0;
            }
    
            else
            {
                printf("\nCould not create file!\n");
            }
    
    
            getchar();
    
        }
    
    
    }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's very well presented code. Easy to study.

    What's the problem you have with it now?

    Edit: looks like jim has made corrections.
    Last edited by Adak; 10-19-2010 at 01:47 PM.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    It would probably help if you were to remove any commented code. It would make the program easier to read.

    Code:
    void newFile()
    {
        FILE *fp;
        char text[TEXTSIZE];
        printf(newline);
        printf(" N E W   F I L E\n");
        char filename[20];
     
     
    
        printf(line);
        printf(newline);
        printf(" Enter the name of the file: ");
        scanf("%s", filename);
     
       fp = fopen(filename, "a");   //////// Open the file here..........
    
        if (fp == NULL)
        {
           printf(" New file failed");
           getchar();
        }
    
        else
        {
            printf(" Enter your text below:\n");
    
            // fp = fopen(filename, "a");     ////////// remove this
    
    //        if (fp != NULL)   ////////////// not needed
    //        {                            /////////////////// not needed
                fgets(text, TEXTSIZE, stdin);
                fputs(text,fp);
                fclose(fp);
    //            return 0;    ////////// not needed
    //        }                      /////////  not needed
    /*                        ///////////////////////////////////////////////// Not needed
            else
            {
                printf("\nCould not create file!\n");
            }
    */
    
            getchar();
    
        }
    
    
    }
    
    
    This should write the to the file on any operating system. 
    
    Jim

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    19
    ok guys. i wil see that. its late here so am going to bed. will try everything you stated tomorrow and will let you know.


    many many thanks.

  14. #14
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    Due to a historical quirk in C, what you have listed under “Function Prototypes” are not actually prototypes; they are old-style declarations. If you want to signify that there are no parameters to a function, you must use (void) instead of (). An empty list means “unspecified number of arguments”. (void) means “no arguments”. This shouldn't be, but for compatibility with older C programs, it must be this way.

    With an empty list, the compiler cannot tell you if you're calling the function incorrectly:
    Code:
    void oldstyle();
    void newstyle(void);
    ...
    oldstyle("foo", 10, 5.5); /* no diagnostic */
    newstyle("foo", 10, 5.5); /* diagnostic required */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OS: How does it work?
    By GReaper in forum Tech Board
    Replies: 15
    Last Post: 08-27-2010, 02:41 PM
  2. detectine OS type?
    By techevo in forum C Programming
    Replies: 4
    Last Post: 03-12-2010, 05:09 AM
  3. Detecting if OS is Windows???
    By Ktulu in forum Windows Programming
    Replies: 2
    Last Post: 11-19-2006, 02:49 AM
  4. a simple OS
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-06-2004, 11:47 PM
  5. How do they compile code for an OS ?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 49
    Last Post: 03-28-2002, 12:16 AM

Tags for this Thread


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