I need to read from an input file by using C programming language to do one of my assignments.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *input = readFromFile(argv);
return 0;
}
char *readFromFile(char *argv[])
{
FILE *fp;
fp = fopen(argv[1],"r");
char *input, c;
int i = 0;
while(!feof(fp))
{
c = fgetc(fp);
input[i++] = c;
}
fclose(fp);
return input;
}
I want to do this reading operation in another function, not in function main(). I tried, but couldn't do it.
When I try to do it with the code above, I get an error message that says:
conflicting types for readFromFile()
How can I fix this error and do what I want?
You have to declare readFromFile before using it. The best way to do this is to add a prototype:
char *readFromFile(char *argv[]); /* note that the identifier is useless here */
NB: By the way, there is a lot of other errors in your source code. The main one is that you don't allocate memory for input. Therefore, you will try to dereference an unitialized pointer: this leads to an undefined behavior. Since you are returning your pointer, you need to use dynamic allocation.
#include <stdlib.h>
char *input = malloc(SIZE);
Moreover, your utilisation of feof is wrong.
First of all you can choose between these:
1. Declare the function prototype;
2. Declare the function before the main.
This way the function is recognized in main.To be fast I always declare function before the main.In readFromFile you aren't allocating the memory that you need, fixed it for you:
#include <stdio.h>
#include <stdlib.h>
char *readFromFile(char *argv[])
{
FILE *fp;
fp = fopen(argv[1],"r");
char *input, c;
int i = 0;
size_t size=100*sizeof(char);
input=(char*)malloc(size);
while( (c = fgetc(fp)) != EOF )
{
if(++i == size)
{
size+= 100*sizeof(char);
input=(char*)realloc(input,size);
}
input[i-1] = c;
}
fclose(fp);
return input;
}
int main(int argc, char *argv[])
{
char *input = readFromFile(argv);
return 0;
}
Related
So I'm trying to read data in from a file and store it in a struct. I'm using fscanf for this, but for some reason it won't enter the while loop at all. It's reading in the file, closing it, and there are no memory leaks. Why could this be happening?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
typedef unsigned long size_t;
typedef struct {
char wt_id[128];
char postcode[128];
int pop_served;
char wt_manager[128];
double x;
double y;
} watchtower_t;
int main(int argc, char **argv)
{
int num_records = 0;
char *record = NULL;
size_t record_len = 512;
int current_size = 5;
//watchtower_t **wt_array = NULL;
watchtower_t temp;
char *filename = NULL;
filename = argv[1];
FILE *file = fopen(filename,"r");
assert(file);
if(argc<2){
return 0;
}
while(fscanf(file, "%[^,],%[^,],%d,%[^,],%lf,%lf",temp.wt_id,temp.postcode,&temp.pop_served,temp.wt_manager,&temp.x,&temp.y)==6){
printf("Enters the while loop");
}
fclose(file);
return 0;
}
sample line from the csv file:
WT3525YPKBW,3525,433,DeniseRoberts,143.32090727836928,-36.14524876420747
I need to write a function that returns every permute of a word in a given text file.
For some reason the output is wrong and I don't really understand why.
However, if instead of writing a function that should check a presnce of a letter (as seen below the function chars(char,char*))
I write the needed letters for check manually
it works as intended.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100
void permute(FILE *fp, char *perm){
int c,i,perm_size=0;
int flag,chars(char,char *);
char permute[MAX];
i=0;
while(perm[i++])
perm_size++;
flag=0;
i=0;
while(!feof(fp)){
c=fgetc(fp);
if(chars(c,perm)==0){/*a function that checks c with each one of the permutes chars*/
flag++;
permute[i++]=c;
if (flag == perm_size){/*if the permute is the word's length*/
permute[i]='\0';
printf("%s\n",permute);
flag=0;
i=0;
permute[0]='\0';
}
}
else{
flag=0;
i=0;
}
}
}
int chars(char ch, char *str){
while(*str++)
{
if(ch==*str)
return 0;
}
return 1;
}
#include "func.h"
#include <string.h>
int main(int argc,char **argv){
FILE *fp;
char *input,*perm;
char *prog=argv[0];
void permute(FILE *, char *);
if(argc==1)
{
fprintf(stderr,"%s error: no arguments\n",prog);
exit(1);
}
input=argv[1];
perm=argv[2];
if(!(fp=fopen(input,"r")))
{
fprintf(stderr,"%s error: cannot open file\n",prog);
exit(1);
}
permute(fp,perm);
fclose(fp);
return 0;
}
input:
./program text chairs
output:
nothing
As told - you should really learn debuging your programs, so I did - I incremented wrongly a value in a function chars(char , char *)
a good pointer explanation from other user
#include <stdio.h>
#include <stdlib.h>
int main (int argc, const char * argv[])
{
FILE *f;
char *chPtr = malloc(sizeof(char)*1000);
/////////////////
f=fopen("input.txt", "r");
if(!f)
return 1;
while (fgets(*chPtr,1000,f)!=NULL) {
printf("%c", *chPtr);
}
fclose(f);
printf("\n%c", *(chPtr+4));
return 0;
}
Our teacher said don't use [] 'brackets' so I'm assuming it's up to malloc, but couldn't make it work?
replace this fgets(*chPtr,1000,f) with this fgets(chPtr,1000,f) and (as #MattMcnabb mentionned)printf("%c", *chPtr) with printf("%s", chPtr)
When trying to read a plain text file with fgets in C, i get some strange looking output on the first line. So if the first line is meant to be "hello" it comes out as something like "ELFh` �� 20120918 (prerelease)#xxhello". Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *fr;
int i;
extern int uniq(char *previous_word, char *current_word);
char *line1 = malloc(500);
char *line2 = malloc(500);
char *temp;
for(i = 0; i<argc; i++)
{
fr = fopen (argv[i], "r");
while(fgets(line2, 499, fr) != NULL)
{
uniq(line1, line2);
temp = line1;
line1 = line2;
line2 = temp;
}
fclose(fr);
}
return 0;
}
int uniq(char *previous_word, char *current_word) {
if(!(current_word))
return 1;
if(strcmp(previous_word, current_word))
printf("%s", current_word);
return 0;
}
I've searched every description i can give of this problem on google and stack overflow and i can find nothing at all that fixes it.
Your loop must begin at index 1. argv[0] is your executable.
To check argv[0] is helpful if you have a so called multi binary executable. There you can handle different commands with just one binary. This is very helpful on embedded systems where you need to save memory.
I've been working on this thing for hours on end and I have searched and searched and my code still does not work right.
How do I read my FILE from a function within main using argv[] as the file that I want read?
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
FILE words(FILE *filesToRead)
{
const char *Open;
Open = (char *)filesToRead;
filesToRead = fopen(Open, "rt");
int line;
while ((line = fgetc(filesToRead)) != EOF)
{
printf("%c", line);
}
fclose(filesToRead);
}
int main(int argc, char *argv[])
{
char *ah = argv[];
words(ah);
return 0;
}
Try this:
void words(char *filename)
{
FILE *filesToRead = fopen(filename, "rt");
/* ... */
}
int main(int argc, char *argv[])
{
if (argc > 1)
words(argv[1]);
return 0;
}
To be honest (and please don't be offended) the way your code looks it seems you have skipped a few chapters in the C book you are using.
argv[] is an array of char *s. For example, if you call your program from the command line as:
my_prog.exe foo bar
Then:
argc will be 3;
argv[0] will point to "my_prog.exe"
argv[1] will point to "foo"
argv[2] will point to "bar"
So inside your main() function, if you are expecting one argument you will need to check the value of argc is 2 and then read your argument out of argv[1].