I need to make a function , which will take as input a .txt file and then proceed
working on each character , while doing a bunch of operations.
This how function's declaration looks like.
int ProcessInput(FILE *in);
The thing is I don't want to take an input from keyboard , but from the .txt file as mentioned before.
I am working on Dev C++ .
Any ideas ?
#include <stdio.h>
int main(int argc, char** argv)
{
if (2 > argc)
{
printf("Please enter a file name on the command line\n");
return 0;
}
FILE* in = fopen(argv[1], "r");
if (!in)
{
printf("Unable to open the specified file\n");
return 0;
}
ProcessInput(in);
fclose(in);
return 0;
}
Related
For example I have a text file includes "I'm having great day!", I want to count each character and word in this file.
in the example "there are 3 a, 1 m" etc.
I'm new at C and know how to open a file, how can find a specific char or word in file but couldn't figure this out. Can you help me pls.
The first thing you need to learn is how to open and process a file one character at a time. You can do this with a program like:
#include <stdio.h>
int main(int argc, char *argv[]) {
// Must provide one argument, the file to process.
if (argc != 2) {
fprintf(stderr, "Usage: myprog <inputFileName>\n");
return 1;
}
// Try to open the file.
FILE *inFile = fopen(argv[1], "r");
if (inFile == NULL) {
fprintf(stderr, "Cannot open '%s'\n", argv[1]);
return 1;
}
// Process file, character by character, until finished.
int ch;
while ((ch = fgetc(inFile)) != EOF) {
putchar(ch); // <accumulate>
}
// Close file and exit.
fclose(inFile);
// <output>
return 0;
}
Then it's a matter of changing the putchar call into whatever you need to do (accumulating character counts) and outputting that information before you exit from the main function.
I'm working on an assignment and I have to open a file from command line upon executing the program.
Example:
program.exe file.txt
However that is not working at all for me. Can someone please tell me what I'm doing wrong? This is the first time I'm working with taking a file as a parameter.
int main(int argc, char **argv) {
int value;
value = fileRead(argv[1]);
}
int fileRead(char argv[]) {
int value;
FILE *fp;
fp = fopen(argv[1], "r");
if (fp) {
fscanf(fp, "%d", &value);
} else {
fprintf(stderr, "Failed to open file!\n");
}
return value;
}
You're mixing up a character and a string. You pass argv[1] to fileRead as argv. Then in fileRead, you do argv[1] again. This effectively does argv[1][1], which just gives the second character of the string. You need to either remove the [1] from in main and then change the argument type, or remove the [1] from fileRead.
While im studying C from a old book (that might be the problem), i wrote the code to copy the content of one file to another one.
But somehow, the program stops working. I would appreciate some help.
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
FILE *fin, *fout; //Pointers to the files
int ch;
if (argc!=3) //Just checking if the user inserted the correct information
{
printf("\nCorrect mode: Program name, file1 -> file2 \n\n");
exit(1);
}
fin=fopen(argv[1], "rb");
if (fin==NULL) //Checking if the file exists
{
printf("\n\nERROR!\n\nThe file you're trying to open does not exist or it cannot be opened.\n\n");
exit(2);
}
if ((fout=fopen(argv[2], "wb"))==NULL) // If it cannot create a file
{
printf("\n\nERROR!\n\nImpossible to create the file %s\n\n", argv[2]);
exit(3);
}
while ((ch=fgetc(fin))!=EOF)
fputs(ch, fout);
fclose(fin);
fclose(fout);
}
You are using fputs to write the characters. It is used for strings (arrays of char). Instead use fputc.
When I compile the program and run it, it does not print anything. I believe the problem is on the while but I can't understand what is wrong. It is supposed to convert hex to ASCII and then the encrypted message.
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(int argc, char **argv) {
int p;
//Opening a file
FILE*tp;
tp = fopen(argv[1], "r");
if(tp == NULL)
{
printf("Error opening file!\n");
return 0;
}
else
{
//Decryption code
while((p=fscanf(" %x",&p))!=EOF)
{
p=p >> 2;
p=p - 200;
printf(" %c",p);
}
}
return 1;
fclose(tp);
}
fscanf() returns number of input items successfully matched and assigned, not input items themselves. Also, as mentioned above, you need to pass file pointer to the function. Try this: while(fscanf(tp, "%x", &p) != EOF)
Looks like you need to specify and provide the FILE pointer "tp" to the fscanf function.
Right now, I have something like this...
CMD console window:
c:\users\username\Desktop> wrapfile.txt hello.txt
Hello
How would I get something like this?
CMD console window:
c:\users\username\Desktop> wrapfile.txt hello.txt hi.txt
Hello Hi
with this code?
#include <stdio.h>
#include <stdlib.h>
int main(int argc[1], char *argv[1])
{
FILE *fp; // declaring variable
fp = fopen(argv[1], "rb");
if (fp != NULL) // checks the return value from fopen
{
int i;
do
{
i = fgetc(fp); // scans the file
printf("%c",i);
printf(" ");
}
while(i!=-1);
fclose(fp);
}
else
{
printf("Error.\n");
}
}
Well, first of all: in your main declaration, you should use int main(int argc, char* argv[]) instead of what you have right now. Specifying an array size makes no sense when declaring an extern variable (that's what argv and argc are). On the top of that, you are not using the correct types. argc is integer and argv is array of strings (which are arrays of chars). So argv is an array of arrays of chars.
Then, simply use the argc counter to loop through the argv array. argv[0] is the name of the program, and argv[1] to argv[n] will be the arguments you pass to your program while executing it.
Here is a good explanation on how this works: http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/#command-line
My 2 cents.
EDIT: Here is a commented version of the working program.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *fp;
char c;
if(argc < 3) // Check that you can safely access to argv[0], argv[1] and argv[2].
{ // If not, (i.e. if argc is 1 or 2), print usage on stderr.
fprintf(stderr, "Usage: %s <file> <file>\n", argv[0]);
return 1; // Then exit.
}
fp = fopen(argv[1], "rb"); // Open the first file.
if (fp == NULL) // Check for errors.
{
printf("Error: cannot open file %s\n", argv[1]);
return 1;
}
do // Read it.
{
c = fgetc(fp); // scans the file
if(c != -1)
printf("%c", c);
} while(c != -1);
fclose(fp); // Close it.
fp = fopen(argv[2], "rb"); // Open the second file.
if (fp == NULL) // Check for errors.
{
printf("Error: cannot open file %s\n", argv[2]);
return 1;
}
do // Read it.
{
c = fgetc(fp); // scans the file
if(c != -1)
printf("%c", c);
} while(c!=-1);
fclose(fp); // Close it.
return 0; // You use int main and not void main, so you MUST return a value.
}
I hope it helps.
argv[2] would be the second file name.
Do not forget to check the value of argc to see if enough arguments are valid.
Better: use boost::program_options.
Caution: this code is not unicode-aware on Windows system, which makes it not portable. Refer to utf8everywhere.org about how to make it support all file names on this platform.