473,385 Members | 1,353 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,385 software developers and data experts.

String-to-textfile programs will not run in DOS window...

Both the programs below fail to run on a Windows DOS window, after
build by MVC/C++ 4.2. No compiler problems, they just refuse to give
me anything but:
"Cannot open input file". Can someone run this and tell me what you
did to make it go? They are from QUE's Crash Course in C, and should
work as flawlessly as the source compiles.
/* WRITER.C - sends characters to text file */
/* as copyrighted - not debugged yet */
#include <stdio.h>
#include <stdlib.h>
#include <process.h>

int main()
{
char ch, filename[85];
FILE *fileptr;

printf("\nEnter filename: ");
gets(filename);

printf("\n\nType # on a blank line to end\n\n");

if ((fileptr = fopen(filename, "w")) == NULL );
{
printf("Error: Cannot open input file\n");
exit(0);
}

while ((ch=getchar()) != '#')
{
putc(ch, fileptr);
}

fclose(fileptr);
return 0;
}

Supposedly maintains a loop which fills up a default keyboard buffer
with every keystroke entered by user, until it is full or until user
termination, when fclose() terminates the program while dumping the
buffer into the disk file.

These programs use functions that read or write an entire line of text
at a time, so that file I/O for strings is as for single characters.

STRTYPE.C reads lines of text (=records???) with the same algorithm as
TYPE.C and displays them. It tracks the lines read and tallies their
number in the data file.

/* STRTYPE.C - Sends characters read from the file to the display
*/

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#define MAXLINELEN 135

int main()
{
char ch, filename[85], strline[MAXLINELEN];
int line = 0;
FILE *fileptr;

printf("\nEnter filename: ");
gets(filename);

if ((fileptr = fopen(filename, "r")) == NULL)
{
printf("Error: cannot open input file\n");
exit(0);
}
while (!feof(fileptr))
{
fgets(strline, MAXLINELEN, fileptr); /* get next char from
file */
printf("%s",strline); /* display char on screen */
line++;
}
fclose(fileptr);

printf("\n***There were %d lines "
"in that file\n", line);

return 0;
}
Nov 13 '05 #1
3 2359

"Blankdraw" <sp********@aol.com> wrote in message

/* WRITER.C - sends characters to text file */
/* as copyrighted - not debugged yet */
#include <stdio.h>
#include <stdlib.h>
#include <process.h>

int main()
{
char ch, filename[85];
FILE *fileptr;

printf("\nEnter filename: ");
gets(filename);

printf("\n\nType # on a blank line to end\n\n");

if ((fileptr = fopen(filename, "w")) == NULL );
For some reason the program cannot open the file for writing. This could be
an OS issue - do you have write permission in the directory you told it to
create the file in? Did you pass it a valid path? Are you sure that you
entered fewer than 84 characters?
printf("Error: Cannot open input file\n");
exit(0);
}

while ((ch=getchar()) != '#')
putc(ch, fileptr);
}

fclose(fileptr);
return 0;
}


Nov 13 '05 #2
MG
if ((fileptr = fopen(filename, "w")) == NULL );


Use if ((fileptr = fopen(filename, "w+")) == NULL );

when you mention "w" as the option for the opening the file, you assume that
the file existed..

HTH
MG
Nov 13 '05 #3
"MG" <an********@hotmail.com> wrote in
<sm*************@read3.inet.fi>:
if ((fileptr = fopen(filename, "w")) == NULL );


Use if ((fileptr = fopen(filename, "w+")) == NULL );

when you mention "w" as the option for the opening the file, you assume that
the file existed..


Sorry, but I definitly have to disagree on that:

r open text file for reading
w truncate to zero length or create text file for writing
a append; open or create text file for writing at end-of-file

r+ open text file for update (reading and writing)
w+ truncate to zero length or create text file for update
a+ append; open or create text file for update, writing at
end-of-file

[quoted from N843-draft]

Irrwahn.

--
Proofread carefully to see if you any words out.
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: David Rubin | last post by:
I looked on google for an answer, but I didn't find anything short of using boost which sufficiently answers my question: what is a good way of doing string tokenization (note: I cannot use boost)....
3
by: Heiko Hund | last post by:
Hi, I do not understand the deeper reason for the following compiler error $ g++ test.cpp test.cpp: In function `int main()': test.cpp:41: error: `std::basic_string<char,...
8
by: wkodie | last post by:
I'm having trouble encrypting/decrypting a simple string using the System.Security.Cryptography.TripleDESCryptoServiceProvider, etc... The encryption works, but the decryption does not properly...
5
by: Pete | last post by:
How do you get rid of leading white spaces in a string in vb.net? From http://www.developmentnow.com/g/38_2005_9_1_0_0/dotnet-languages-vb.ht Posted via DevelopmentNow.com Group...
2
by: PioM | last post by:
namespace System { public class Text { public static System.Char Omin='\t',Next=' ',Wciecie='\"',Zamkniecie='\"',Null='\0',Object='ÿ'; public static System.Int32 NextCharacter(System.Int32...
10
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not...
2
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
15
by: Lighter | last post by:
I find a BIG bug of VS 2005 about string class! #include <iostream> #include <string> using namespace std; string GetStr() { return string("Hello");
2
by: alacrite | last post by:
With this testprogram: #include <iostream> #include <string> using namespace std; int main() {
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.