473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Problems with file input/output

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:
Expand|Select|Wrap|Line Numbers
  1. FILE *asciivalue_file;
  2. asciivalue_file = fopen("asciivalue.txt","w");
  3. fseek(asciivalue_file, 0, SEEK_SET);
  4. char chartestarray[charcount];
  5. //Charcount is devised elsewhere and works fine
  6.  
  7. int readin = 2;
  8. int i = 0;
  9.  
  10. while (readin != 7)
  11. //Very crude way of repeating I know
  12. {
  13.      lc = fgetc(asciivalue_file);
  14.      if (lc == EOF)
  15.           break;
  16.      else
  17.           if (lc != EOF)
  18.           {
  19.                chartestarray[i] = lc;
  20.                i++;
  21.           }
  22. }
  23.  
Hope that makes sense :(
Nov 25 '07 #1
3 1411
After taking a fast look, should not you be opening the file in read mode rather than write mode?
Expand|Select|Wrap|Line Numbers
  1. FILE *asciivalue_file;
  2. asciivalue_file = fopen("asciivalue.txt","w");//<--- should be "r"
  3.  
Also, when reading the characters and saving them into the array of characters, you should add the end of string.
Expand|Select|Wrap|Line Numbers
  1. i=0;
  2. while (i != 7)
  3. {
  4.      lc = fgetc(asciivalue_file);
  5.      if (lc == EOF)
  6.           break;
  7.      chartestarray[i++] = lc;
  8.      chartestarray[i]=0;
  9. }
  10.  
Nov 26 '07 #2
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.
Nov 26 '07 #3
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.
Nov 26 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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/ ....
4
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...
7
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...
1
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...
6
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...
2
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...
1
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:...
0
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...
24
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...
10
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.