Just to give you some background, I am trying to write an encryption algorithmn, so the options are -e to encrypt or -d to decrypt. During the encryption process an array of characters is written to a file called asciivalue.txt and stored for later use. This part of the program appears to work fine, HOWEVER when I try to read back from the file during the decryption process the input I am getting is not what I expected. The input file contains "YYYYYNYYYYY" but when I read from the file into the array (chartestarray) it appears as random characters i.e. smilie faces, spaces etc......
I have read other inputs/outputs in the same program using the same logic and coding and and they work perfectly, so I am really confused why this isn't working!?!
The way I am doing it at the moment is: -
FILE *asciivalue_file;
-
asciivalue_file = fopen("asciivalue.txt","w");
-
fseek(asciivalue_file, 0, SEEK_SET);
-
char chartestarray[charcount];
-
//Charcount is devised elsewhere and works fine
-
-
int readin = 2;
-
int i = 0;
-
-
while (readin != 7)
-
//Very crude way of repeating I know
-
{
-
lc = fgetc(asciivalue_file);
-
if (lc == EOF)
-
break;
-
else
-
if (lc != EOF)
-
{
-
chartestarray[i] = lc;
-
i++;
-
}
-
}
-
Hope that makes sense :(
3 1411
After taking a fast look, should not you be opening the file in read mode rather than write mode? -
FILE *asciivalue_file;
-
asciivalue_file = fopen("asciivalue.txt","w");//<--- should be "r"
-
Also, when reading the characters and saving them into the array of characters, you should add the end of string. -
i=0;
-
while (i != 7)
-
{
-
lc = fgetc(asciivalue_file);
-
if (lc == EOF)
-
break;
-
chartestarray[i++] = lc;
-
chartestarray[i]=0;
-
}
-
Many thanks, your first point pointed me in the right direction.
I was opening asciivalue.txt using write because if the user chooses -e (to encrypt) then an array of characters is written to that file. If they choose -d (to decrypt) then the characters are read from the file to an array of characters.
I was declaring this at the start of the program along with my variables, I assumed that if I opened it as write then it would also read i.e. work O.K in both functions. I'm guessing that opening with WRITE access does not necessarily give the user READ access?
I have now ameneded the code so that it the user chooses -e then asciivalue.txt is opened with write access, however if they choose -d then asciivalue.txt is opened with read access. It is now functioning as expected.
Correct, if using "w" you can only write, is using "r" you can only read. I dont know whether there is a way to open files in Read and Write mode using C standard file functions.
Win32 file functions allow opening files in read AND write mode though.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jerome Lefebvre |
last post by:
Hello,
Hope this will interest a few. I been working with a friend on the
problems given out during the "International Collegiate Programming Contest"
(ICPC) http://icpc.baylor.edu/icpc/ ....
|
by: will |
last post by:
Trying to modify some code from :
http://www.faqts.com/knowledge_base/view.phtml/aid/2357
But having some problems. Cant work out how/why its not adding rows in
the right place. seems strange to...
|
by: kamkwokho |
last post by:
Could any answer following questions as many as you can.
ii) Write a C shell script convertmin which will read in a number, thought
of as representing minutes, and print out the number of...
|
by: Jamal |
last post by:
I am working on binary files of struct ACTIONS
I have a recursive qsort/mergesort hybrid that
1) i'm not a 100% sure works correctly
2) would like to convert to iteration
Any comments or...
|
by: Jamal |
last post by:
I am working on binary files of struct ACTIONS
I have a recursive qsort/mergesort hybrid that
1) i'm not a 100% sure works correctly
2) would like to convert to iteration
Any comments or...
|
by: Bonnett |
last post by:
I have made a simple console application that creates a webpage from my
playlist, then uploads it to my webhost. I decided to convert it to a
windows service and use the FileSystemWatcher to...
|
by: electrixnow |
last post by:
Help!,
I need to compile this code with static libs so it run on another XP
machine that does'nt have
MS Studio installed. When I compile now I get an ERROR:
1>------ Rebuild All started:...
|
by: ben |
last post by:
OK so there are a few things going on. I succesfully created a SOAP
webreference in VS IDE which when built works fine under small amounts
of load, however under heavy amounts of load we start to...
|
by: allpervasive |
last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems
in reading and modifying the contents of a file,, hope you can help to
solve this problem. Here i attach the file to be...
|
by: lancer6238 |
last post by:
Hi all,
I'm having programs reading from files.
I have a text file "files.txt" that contains the names of the files to
be opened, i.e. the contents of files.txt are
Homo_sapiens.fa...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |