473,738 Members | 8,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C Program [ Turbo-C ] , to extract only-Printable-characters from a file ( any type of file) and display them ( i.e equivalent to "strings" command in UNIX)

Hi

I am creating a C Program [ Turbo-C ] , to extract
only-Printable-characters from a file ( any type of file) and display
them.
OS: Windows-XP

Ple help me to fix the Errors & Warnings and explain how to use
Command-Line Arguments inside C program.

thanks
SunRise

--------------------------------------------------------
Warnings & Errors:

WARNING : Non-portable pointer compariosn in function main
Error(1) : Illegal structure operation in function main
Error(2) : Illegal structure operation in function main
Error(3) : Illegal structure operation in function main
Error(4) : type mismatch in parameter 'c' in call to "_fputc" in
function main*/

--------------------------------------------------------
#include <stdio.h>
#include <ctype.h>
#include <process.h>
void main()
{

/*char *file1 = argv[1]; */

FILE *fp1;

fp1=fopen("c:\\ tmp\\tmp\\CPUCo unt20.exe","r") ;

if ( fp1 == NULL ) /* WARNING is here */
{
/* printf("Error opening file : %s\n\n", file1); */
printf("Exiting ...\n");
exit(1) ;
}

while( fp1 != EOF )
{

if( isprint(*fp1) ) /* Error(1) & Error(2) */
{
putc(*fp1,stdou t); /* Error(3) & Error(4) */
}
fp1++;
}
printf(" ======= End of strings1 ======== \n\n");
}

Nov 15 '05 #1
2 4196
SunRise wrote:
Hi

I am creating a C Program [ Turbo-C ] , to extract
only-Printable-characters from a file ( any type of file) and display
them.

Ple help me to fix the Errors & Warnings and explain how to use
Command-Line Arguments inside C program.
#include <stdio.h>
#include <ctype.h>
#include <process.h> ^^^^^^^^^^ There is no such standard header

void main() ^^^^ main always returns an int {

/*char *file1 = argv[1]; */

FILE *fp1;

fp1=fopen("c:\\ tmp\\tmp\\CPUCo unt20.exe","r") ;

if ( fp1 == NULL ) /* WARNING is here */
{
/* printf("Error opening file : %s\n\n", file1); */
printf("Exiting ...\n");
exit(1) ; ^^^ The portable arguments for exit()
are EXIT_FAILURE, EXIT_SUCCESS, and 0 }

while( fp1 != EOF ) ^^^ this, and all subsequent references to fp1 are nonsense. {

if( isprint(*fp1) ) /* Error(1) & Error(2) */
{
putc(*fp1,stdou t); /* Error(3) & Error(4) */
}
fp1++;
}
printf(" ======= End of strings1 ======== \n\n");


Start by turning on all the warnings you can.
Then write this as a simple filter. You will then learn to get the
skeleton right:

/* filter version, using getchar() */

#include <stdio.h>
#include <ctype.h>

int main(void)
{
int c;
while ((c = getchar()) != EOF)
if (isprint(c) || (c == '\n'))
putchar(c);
putchar('\n');
return 0;
}
Now, replace the call to getchar() with a call to fgetc(). This will
show you how to implement the processing loop with file for input:

/* filter version, using fgetc() */

#include <stdio.h>
#include <ctype.h>

int main(void)
{
int c;
while ((c = fgetc(stdin)) != EOF)
if (isprint(c) || (c == '\n'))
putchar(c);
putchar('\n');
return 0;
}

Now, let's add the possibility of a single command line argument for the
input file's name. We must decide what to do when
1) the argument is missing
2) when the argument is present
3) when there are too many argumnents
Here I have chosen to modify the filter version so it will continue to
function without a command line argument:

/* version allowing either 0 or 1 command line arguments */

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int c;

/* test for too many arguments */
if (argc > 2) {
fprintf(stderr, "too many command line arguments.\n");
exit(EXIT_FAILU RE);
}

/* handle exactly one argument by reopening stdin */
if (argc == 2)
if (!freopen(argv[1], "rb", stdin)) {
fprintf(stderr, "Could not open \"%s\" for input.\n"
"Bailing out ...\n", argv[1]);
exit(EXIT_FAILU RE);
}
/* and if there is no argument, use existing stdin. */

while ((c = fgetc(stdin)) != EOF)
if (isprint(c) || (c == '\n'))
putchar(c);
putchar('\n');
return 0;
}
You could obviously modify this in other ways. For example, you could
decide to require the command line argument and decide to open a new
input file instead of reopening stdin. Then you would need to have a
FILE pointer:
FILE *fp;
and use fopen() instead of freopen
if (!(fp = fopen(argv[1], "rb")))
and replace fgetc(stdin) with fgetc(fp)
and change the handling of the case with no arguments.
Nov 15 '05 #2
Thanks CBFalconer for the links.

Thanks a lot to Martin for the detailed analysis and sugestions /
explanations.

Regds
SunRise

Software Engineering Consultant

www.Geocities.com/Explore_Clearcase

Nov 15 '05 #3

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

Similar topics

1
5180
by: hokiegal99 | last post by:
This is not really a Python-centric question, however, I am using Python to solve this problem (as of now) so I thought it appropiate to pose the question here. I have some functions that search for files that contain certian strings and if the files found to have these string do not already have a filename extension (such as '.doc' or '.xls') the function will append that to the files and rename them. So, if a file named 'report' was...
2
8872
by: Brian Henry | last post by:
I want to list out a directory listing along with showing the file type name (like explorer does when it says something like "MyDoc.DOC - Microsoft Word Document" How do I get that file type name which is Microsoft Word Document based on the extension or what ever it needs to figure out the type so i can display it in a program? also is there a simple way to get the icon of the file type? I've seen complex ways before, but is there any...
1
1210
by: VB Programmer | last post by:
I have a custom file type ("PWA" extension.) It's just a text file with some text created by the web server. When my web page redirects to one of these pages I want to auto launch a local app. (This action similar to PDF files. When I access a PDF file in IE it opens it automatically in Acrobat.) I created the file association manually, but when I response.redirect to the PWA file it displays it in IE, rather than open it using my...
15
4775
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update button will verify the information that has been entered and updates the data base if the data is correct. Update will throw an exception if the data is not validate based on some given rules. I also have a custom error handling page to show the...
3
2433
by: Mark Gibson | last post by:
Is there an equivalent to the unix 'file' command? $ file min.txt min.txt: ASCII text $ file trunk trunk: directory $ file compliance.tgz compliance.tgz: gzip compressed data, from Unix What I really want to do is determine if a file is 1) a directory, 2) a
8
2839
by: Fabian Braennstroem | last post by:
Hi, I would like to remove certain lines from a log files. I had some sed/awk scripts for this, but now, I want to use python with its re module for this task. Actually, I have two different log files. The first file looks like: ...
0
1040
Blade
by: Blade | last post by:
Hello Friends well this is my first post on this site, hope i get the solution for my problem During the development of one project i am facing this problem i use a SHGetFileInfo structure to get icons for files and folders, it work fine but in same program i use SHGetFileInfo again to get a file type as it is display in windows explorer. this time i make a vb class file and call it whenever i req. to display a file type, but it shows some...
2
1497
by: jack | last post by:
Hi, Im creating a program in which im trying display a file list in the listview. similar as windows explorer detailed view. im able to get every thing except the file type. im stuck here and could not file any example on net. Please help
1
3069
by: veer | last post by:
Hi i am making a program in which i want to extract data from html file . Actually there are two dates on html file i want to extract these dates but the main probleum is that these dates are different on each file. A word "AKTIVA" is always comes before these dates. i made this by seaching the activa word but after this i am not getting any idea how these dates can be accessed. i use one another method by transfering the whole data of...
65
5085
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but got errors of Failed to open file ./outdir/mytestout.txt. Below is the code: #include <stdio.h>
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.