PDA

View Full Version : Basic c programming help




Rudd999
May 10, 2013, 03:50 PM
hey guys, i dont get why i cant find a .txt file :( it so frustrating because i dont get why it cant find the file. i have tried it in all folders of the project and it just wont find it.

im using c btw :P

copy of the code:

{
char ch, ch2;
FILE *fp1, *fp2;
fp1=fopen("MyEncryptedFileA.txt","r");
if (fp1 == NULL)
{
printf("ERROR no file\n");
ch = getchar();
exit(1);
}
printf("File Decrypted.... \n");
fp2=fopen("MyDecryptedFileA.txt","w");

ch = fgetc(fp1);
while(ch != EOF)
{
/* decryption algorithm a here */
ch2 = ch - 10;
/* end of decryption algorithm */
fprintf(fp2, "%c", ch2);
ch = fgetc(fp1);
}
fclose(fp1);
fclose(fp2);
printf("\n**********************\n");
printf("\n\n");
}



ArtOfWarfare
May 10, 2013, 04:06 PM
I find file handling to be a rather complicated topic that shouldn't be taught early on, mostly because programmers rarely need to handle files.

Having said that, what's the present working directory when your program is running?

Are you using Xcode? If so, you need to make sure that in your Build Phases the file you'd like to access in your program is copied. To access Build Phases, click on your project in the navigator (left panel of Xcode,) then on your target (next panel to the right that opens when you select it,) then select the "Build Phases Tab". You might need to add a new build phase of the type Copy Files. Drag in the file that you want copied. The destination should be "Products Directory" if I recall correctly. Make sure "Copy only when installing" is unchecked.

Rudd999
May 10, 2013, 04:14 PM
i dont know if this is going to help but thought it might show you what i have done so far :/ i have written the code myself and i understand how to write c but not how to link files in xcode

subsonix
May 10, 2013, 04:23 PM
Where is your "MyEncryptedFileA.txt" located? From the looks of it, not in your current working directory.

Rudd999
May 10, 2013, 05:03 PM
the program creates the file. that is the point :P it reads the MyFile.txt file, ascii by ascii and adds 10 to the amount and then writes the new ascii to the new MyEncryptedFileA.txt file.

subsonix
May 10, 2013, 05:08 PM
the program creates the file. that is the point :P it reads the MyFile.txt file, ascii by ascii and adds 10 to the amount and then writes the new ascii to the new MyEncryptedFileA.txt file.

Right, you changed you code. Because your first version in post #1 sure attempts to opens the file for reading. Creating a caesar cipher, wouldn't you want to use any file as input?

Still, you need to set your working directory to where the file is located (the file you can not open, previously MyEncryptedFileA.txt).

ArtOfWarfare
May 10, 2013, 08:52 PM
Your cases don't match - that could be contributing to the issue. Your file (listed in the left sidebar) has camel case while in your code you have all lowercase letters. I'm not sure if fopen() is case sensitive but I'd say it's safer to guess it is and just match case just to rule that out as a potential issue.