Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Rudd999

macrumors newbie
Original poster
May 10, 2013
3
0
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:

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");
}
 
Last edited by a moderator:

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
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

macrumors newbie
Original poster
May 10, 2013
3
0
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
 

Attachments

  • Screen Shot 2013-05-10 at 22.12.10.png
    Screen Shot 2013-05-10 at 22.12.10.png
    1.5 MB · Views: 103

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Where is your "MyEncryptedFileA.txt" located? From the looks of it, not in your current working directory.
 

Rudd999

macrumors newbie
Original poster
May 10, 2013
3
0
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

macrumors 68040
Feb 2, 2008
3,551
79
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

macrumors G3
Nov 26, 2007
9,558
6,058
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.