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();
}
}