Edit: as requested I've included the code in 'ledic'. However, it never ran - any of it, not even a hello world printf as first line, so I am relatively sure the problem would never be withinit.
Edit2: ironically enough, it was within the 'ledic' function. Looks like I understand even less about this than I previously thought.
I am writing for my current project at Uni and no one around me can figure out this segmentation fault. It should be pretty straightforward so I appreciate your help.
Code as follows:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void ledic(FILE *fp){
printf("Hello world\n");
int len;
int j, i, k;
char palavra[30];
char dictionary[30][10000][30];
int VecOcorrencias[30];
for (j=0; j<30; j++)
VecOcorrencias[j]=0;
printf("Hello world\n");
while ( fscanf(fp, "%s", palavra) == 1 ) {
len = strlen(palavra);
k = VecOcorrencias[len];
strcpy (dictionary[len][k], palavra);
VecOcorrencias[len]++;
}
for (i=0; i<1000; i++)
printf("%s %d\n", dictionary[5][i], VecOcorrencias[5]);
}
}
FILE *OpenFile( char *nome, char *mode){
FILE *fp;
fp = fopen (nome, mode);
if( fp == NULL){
printf(" Cant open file\n");
exit(1);
}
return (fp);
}
int main( int argc, char *argv[]){
FILE * fp;
fp = OpenFile( argv[1], "r");
ledic(fp);
return(0);
}
The code breaks when it enters the void ledic(FILE *fp) function, and says it cannot access the referenced memory (I suppose *fp).
I cannot for the life of me figure out why. Any thoughts?
The declaration char dictionary[30][10000][30]; creates a 9Mb variable 30*30*1000 = 9'000'000. As it is a local variable it is created on the stack and the default stack size on a typical Linux machine is only 8Mb (on Windows it's even only 1Mb).
If you declare it as static, the variable is not on the stack and therefore it can take more memory than the stacksize.
See this SO article for more details on the static keyword.
Related
So, I'm getting a really weird error in my program, and I've narrowed it down to something to do with getline() and declaring an array of size greater than 8. However, I'm super confused as to why it's doing that, so any help or explanation is greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
void populateConfig();
int main(int arc, char *argv[])
{
// when thing is size 8 it works fine
char thing[9];
populateConfig();
return 0;
}
void populateConfig(){
FILE *fp;
char string1[1000], string2[1000];
char *line;
int len = 0;
fp = fopen("ws.conf", "r");
while(fscanf(fp, "%s", string1) != -1){
// Commenting out if statement prevents crash
if(string1[0] == '#'){
getline(&line, &len, fp);
continue;
}
}
fclose(fp);
}
So I'm just reading in a config file, and as shown, when I change the size of thing to 8 or less, it works, and when I comment out the if statement in populateConfig() it also works. Is this something to do with the stack or memory? Should I do something differently?
You need to initialize char *line to NULL first. Then before returning, free it.
https://linux.die.net/man/3/getline
#include <stdio.h>
#include <stdlib.h>
struct fileIndex{
char name;
int key;
} index1;
int main(int argc, char *argv[]){
int i;
FILE *pFile;
pFile= fopen("cat/home/sysadmin/deneme.txt","r");
for(i=0; i<10; i++){
printf("%c",fgetc(pFile));
}
fclose(pFile);
}
When I want to run my program, it gives that error. I looked so long for a wrong line in code, but I didn't find any. Can you help me ?
If the file failed to open, that will make pFile equal NULL, which can easily cause fgetc() to segfault.
You must check for this before trying to read from the file:
if (pfile == NULL)
{
perror("Failed to open file");
exit(1);
}
change your code as
int i;
FILE *pFile;
pFile= fopen("cat/home/sysadmin/deneme.txt","r");
if(!pFile)
return;
Also.. looks like you file path is misplaced... are your meant t ouse /cat/home/sysadmin/deneme.txt
Is cat your current directory or part of absolute path
i am trying to create an program to generate empty files. but when it try to run the program it crashes after taking inputs from the console .
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int create(char* filename)
{
char filext[10];
printf("\nEnter File Extension :");
fgets(filext);
FILE* fp;
fp = fopen(strcat(filename,strcat(".",filext)),"w");
if(!fp)
{
return 0;
}
fclose(fp);
return 1;
}
int main(int argc , char* argv[])
{
int f;
int i;
char buffer[33];
if (argc == 3)
{
for(i = 0; i < atoi(argv[2]) ; i++)
{
f = create(strcat(argv[1],itoa(i,buffer,10)));
if(f==0)
{
printf("error in creating files . check uac!!!");
}
else{
printf("\nfile Created ...\n");
}
}
}
else{
printf("syntax Error");
}
return 0;
}
when I try to run this program I get the following output
F:\selfcreatedtools\filegen>gcc gen.c
F:\selfcreatedtools\filegen>a level 100
Enter File Extension :php
after entering the extension the program crashes.
i am a beginner in c programming.
Your main problem lies in the strcat(".",filext) part of fp = fopen(strcat(filename,strcat(".",filext)),"w");
Try
strcat(filename, ".");
strcat(filename, filext);
fp = fopen(filename, "w");
And it might be better if the function definition header was made
int create(char filename[SIZE]) (where SIZE is a value less than the size filename will be) instead of int create(char* filename) since you are using strcat() to modify the string in the user-defined function create(). You wouldn't want illegal memory accesses that would cause errors if the string encroaches upon the memory allotted to something else.
A similar problem is there with using strcat() to modify the string at argv[1] as pointed out by Jonathan Leffler for which BLUEPIXY has provided a solution in the comments.
I need to write a code that will print the frequency of each word from a given file. Words like "the" and "The" will count as two different words. I've written some code so far but the command prompt stops working when I try to run the program. I just need some guidance and to be pointed in the best direction for this code, or I would like to be told that this code needs to be abandoned. I'm not very good at this so any help would be very appreciated.
#include <stdio.h>
#include <string.h>
#define FILE_NAME "input.txt"
struct word {
char wordy[2000];
int frequency;
} words;
int word_freq(const char *text, struct word words[]);
int main (void)
{
char *text;
FILE *fp = fopen(FILE_NAME, "r");
fread(text, sizeof(text[0]), sizeof(text) / sizeof(text[0]), fp);
struct word words[2000];
int nword;
int i;
nword = word_freq(text, words);
puts("\nWord frequency:");
for(i = 0; i < nword; i++)
printf(" %s: %d\n", words[i].wordy, words[i].frequency);
return 0;
}
int word_freq(const char *text, struct word words[])
{
char punctuation[] =" .,;:!?'\"";
char *tempstr;
char *pword;
int nword;
int i;
nword = 0;
strcpy(tempstr, text);
while (pword != NULL) {
for(i = 0; i < nword; i++) {
if (strcmp(pword, words[i].wordy) == 0)
break;
}
if (i < nword)
words[i].frequency++;
else {
strcpy(words[nword].wordy, pword);
words[nword].frequency= 1;
nword++;
}
pword = strtok(NULL, punctuation);
}
return nword;
}
First off all:
char *text;
FILE *fp = fopen(FILE_NAME, "r");
fread(text, sizeof(text[0]), sizeof(text) / sizeof(text[0]), fp);
Reads probably 4 bytes of your file because sizeof(text[0]) is 1 and sizeof(text) is probably 4 (depending on pointer size). You need to use ftell() or some other means to get the actual size of your data file in order to read it all into memory.
Next, you are storing this information into a pointer that has no memory allocated to it. text needs to be malloc'd or made to hold memory in some way. This is probably what is causing your program to fail to work, just to start.
There are so so SO many further issues that it will take time to explain them:
How you are using strcpy to blow up memory when you place it intotempstr
How even if that weren't the case, it would copy probably the whole file at once, unless the file had NULL terminated strings within, which it may, so perhaps this is ok.
How you compare nwords[i].wordy, even though it is not initialized and therefore garbage.
How, even if your file were read into memory correctly, you look a pword, which is unitialized for your loop counter.
Please, get some help or ask your teacher about this because this code is seriously broken.
I am learning C and I have tried to build a program that outputs its own source. This is my source:
#include <stdio.h>
int S = 512;
int main(){
FILE * fp;
fp = fopen("hello.c","r");
char * line = (char *) malloc(S);
int i = 0;
while (i == 0)
{
i = feof(fp);
printf("%s",line);
fgets(line,S,fp);
}
fclose(fp);
}
I have used the tcc compiler and I got this output:
But notice, I got a hyphen before #include. The rest of the output is correct.
So please can someone explain why I got this hyphen??
You're printing the first line before you've read anything.
#include <stdio.h>
int main(){
FILE *fp = fopen("hello.c", "r");
char line[256];
while (fgets(line, sizeof line, fp) != NULL)
printf("%s",line);
fclose(fp);
return 0;
}
#ooga gave you the correct answer.
The why is that malloc doesn't initialize the memory before it returns it to you, unlike its sister calloc.
Most likely, on another platform / compiler, you'd get something different.
Some compilers use a debug heap that initializes "unitialized" memory to a specific value. The release mode will probably result in random garbage instead of a '-' everytime.