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

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\\CPUCount20.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,stdout); /* Error(3) & Error(4) */
}
fp1++;
}
printf(" ======= End of strings1 ======== \n\n");
}

Nov 15 '05 #1
2 4155
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\\CPUCount20.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,stdout); /* 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_FAILURE);
}

/* 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_FAILURE);
}
/* 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
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...
2
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...
1
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. ...
15
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...
3
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 ...
8
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...
0
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...
2
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...
1
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...
65
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...
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.