I have created a program that reads a series of strings from a .txt file and after compiling a new .txt file is created where the strings should be in alphabetical order.The problem is that I can't write more than 10 words, the compiler just stops/crashes, WHY? Does it depend by the type of compiler? I am currently using Code-Bloks.How can I optimize the code to run more smoothly?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void arrange(int n, char *x[])
{
char *temp;
int i,str;
for(str = 0; str < n-1; ++str)
{
for(i = str+1; i < n; ++i)
{
if(strcmp(x[str],x[i]) > 0)
{
temp = x[str];
x[str] = x[i];
x[i] = temp;
}
}
}
return;
}
int number_of_lines = 0;
void countOfLinesFromFile(char *filename){
FILE* myfile = fopen(filename, "r");
int ch;
do
{
ch = fgetc(myfile);
if(ch == '\n')
number_of_lines++;
}
while (ch != EOF);
if(ch != '\n' && number_of_lines != 0)
number_of_lines++;
fclose(myfile);
return number_of_lines;
}
int main()
{
int i , ts=0;
char *x[10];
char *fileName = "WORDS.txt";
countOfLinesFromFile(fileName);
printf("%d",number_of_lines);
FILE * fp;
fp = fopen ("WORDS.txt", "r");
for(i = 0; i < number_of_lines; i++)
{
x[i] = (char*) malloc (1200*sizeof(char));
fscanf(fp, "%s", x[i]);
}
FILE *fPointer;
fPointer=fopen("Alphabetical.txt","w+");
arrange(i,x);
for(i = 0; i < number_of_lines; i++)
{
fprintf(fPointer,"%s\n",x[i]);
}
fclose(fPointer);
fclose(fp);
return 0;
}
char *x[10];
The buffer size is too small
These two lines define how much information you can store
char *x[10]; // 10 strings
x[i] = (char*) malloc (1200*sizeof(char)); // 1200 characters each
As it is written now, you can only hold a maximum of 10 strings with each string being no longer than 1200 characters.
The crash is caused when number_of_lines >= 11 in the following for loop:
for(i = 0; i < number_of_lines; i++)
{
x[i] = (char*) malloc (1200*sizeof(char));
fscanf(fp, "%s", x[i]);
}
When i is 11 you write to x[11] which is past the end of x.
Related
I have following data in a file:
Name
Surname
#include <stdio.h>
#define FILENAME "file.txt"
#define MAXSIZE 128
int main(void)
{
setvbuf(stdout, NULL, _IONBF, 0);
FILE *file = fopen(FILENAME, "r");
if (!file) {
perror(FILENAME);
return 1;
}
int ch;
size_t i = 0;
char array[3][MAXSIZE];
for(int a=0; a < 3; a++)
{
while (i < MAXSIZE - 1 && ((ch = getc(file)) != EOF))
{
if (ch == '\n')
break;
array[a][i++] = ch;
}
/* null-terminate the array to create a string */
array[a][i] = '\0';
}
fclose(file);
for(int a=0; a < 3; a++)
{
for(int i=0; i < 10; i++)
{
printf("%c", array[a][i]);
}
}
}
When I run this program it gives me output like this "
How can I modify it, so it will not output garbage?
This is a link to my previous post:
Link
As I noted in a comment:
Your printing loop for (int i=0; i < 10; i++) { printf("%c", array[a][i]); } needs to stop when array[a][i] == '\0' —— add if (array[a][i] == '\0') break; before the printf().
You also need to reset i to 0 before the while loop (but after the for loop). If you declared i inside the first for loop, you'd not have the problems you do.
Note that you have two different variables called i (one is size_t i = 0; before the loops; the other is for (int i = 0; …) while printing) and one hides the other. That can lead to confusion.
Those changes might lead to this code:
#include <stdio.h>
#define FILENAME "file.txt"
#define MAXSIZE 128
int main(void)
{
setvbuf(stdout, NULL, _IONBF, 0);
FILE *file = fopen(FILENAME, "r");
if (!file) {
perror(FILENAME);
return 1;
}
char array[3][MAXSIZE];
for (int a = 0; a < 3; a++)
{
int ch;
size_t i = 0;
while (i < MAXSIZE - 1 && ((ch = getc(file)) != EOF))
{
if (ch == '\n')
break;
array[a][i++] = ch;
}
array[a][i] = '\0';
}
fclose(file);
for (int a = 0; a < 3; a++)
{
for (int i = 0; i < 10; i++)
{
if (array[a][i] == '\0')
break;
printf("%c", array[a][i]);
}
}
}
There's also no obvious reason not to print the data using:
for (int a = 0; a < 3; a++)
puts(array[a]);
or
for (int a = 0; a < 3; a++)
printf("%s\n", array[a]);
I have a program, which receives filename as an input, saves file contents into 2d char array and then outputs words. It works absolutely fine for about 400 words, but then, when I add more words, it crashes. Debugging showed that i am trying to access unused address, and I don't understand how is that possible considering that previous tests with lesser amount of words were successful.
The question is: what am i missing here?
FILE: functions.c
#include "Lab10.h"
#include <stdio.h>
#include <malloc.h>
char** parser(char* filename) {
FILE* fp;
fp = fopen(filename, "r");
char** str = (char**)calloc(N, sizeof(char*) * N);
if (!str)
{
printf("\n Allocation error");
return NULL;
}
char ch;
int space = 0, words = 0;
for (int i = 0; !feof(fp); i++) // Memory allocation
{
ch = fgetc(fp);
if (!is_ch(ch))
{
if (i != space)
{
if (!(str[words] = (char*)calloc(i - space, sizeof(char) * (i - space))))
{
printf("\n Allocation error");
return NULL;
}
words++;
}
while (!is_ch(ch) && !feof(fp))
{
ch = fgetc(fp);
i++;
}
if(!feof(fp))
fseek(fp, -(int)sizeof(char), 1);
i--;
space = i;
}
}
fseek(fp, 0, SEEK_SET);
for (int i = 0; i < words; i++) // Copying words into 2d array
{
while (!is_ch(fgetc(fp)));
if (!feof(fp))
fseek(fp, -(int)sizeof(char), 1);
int j = 0;
do {
if (((fscanf(fp, "%c", &str[i][j])) != 1))
break;
j++;
} while (is_ch(str[i][j-1]) && !feof(fp));
}
return str;
}
int is_ch(char ch)
{
return ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'));
}
FILE: main.c
#define _CRT_SECURE_NO_WARNINGS
#include "Lab10.h"
#include <stdio.h>
#include <stdlib.h>
int main() {
char* filename = (char*)malloc(sizeof(char) * N);
if (!scanf("%s", filename) || filename == 0)
{
printf("\n Incorrect filename input");
return -1;
}
char** str = parser(filename);
printf("\n Contents of .txt file:");
for (int i = 0; str[i] != NULL; i++) {
printf("\n\t%d) ", i+1);
for (int j = 0; is_ch(str[i][j]); j++) {
printf("%c", str[i][j]);
}
}
return 0;
}
This answer was posted as a reply to one of the comments below the question itself. I tried writing readWord function, which recieves filepointer, reads one word and then returns pointer to the resulting array - that's eases the procedure, making it less complex. It works almost like fgets(), but it reads till non-character, instead of a newline
readWord function itself:
char* readWord(FILE* fp) {
char ch = 0;
while (!is_ch(ch))
{
ch = fgetc(fp);
if (ch == EOF || !ch)
return NULL;
}
int size = 1;
while (is_ch(ch))
{
if ((ch = fgetc(fp)) == EOF || !ch)
break;
size++;
}
fseek(fp, -(size * (int)sizeof(char)), 1);
if (ch != EOF || !ch)
size--;
char* word = (char*)calloc(size, sizeof(char) * size + 1);
if (!word)
{
printf("\n Allocation error.");
return NULL;
}
for (int i = 0; i < size; i++)
word[i] = fgetc(fp);
word[size] = '\0';
return word;
}
That's how i use it in main():
FILE* fp = fopen("test.txt", "r");
char* word;
while ((word = readWord(fp)) != NULL)
{
for (int i = 0; word[i] != '\0'; i++)
printf("%c", word[i]);
printf(" ");
}
Is there is anything i need to improve here? It works fine, but is it possible to somehow make it better?
Currently I am making a project that uses char arrays that have null elements. I want to be able to get the length of the array, in the sense of the number of elements that aren't null. This seemed reasonably trivial and I made this function:
int getWordLen(char word[]) {
int count = 0;
for (int i = 0; i < 512; i++) {
if (word[i] != '\0') {
count++;
}
}
printf("%d ", count);
return count;
}
However, every char array returns a length of 188. Any help would be appreciated.
This is the function I was calling it from:
void redact(Words * redactWords, char fileName[]) {
FILE * file = fopen(fileName, "r");
FILE * outputFile = fopen("outputFile.txt", "w+");
char word[512];
int i = 0;
char c;
while (c != EOF) {
c = getc(file);
if ((c > 96) && (c < 123)) {
word[i] = c;
i++;
continue;
}
else if ((c > 64) && (c < 91)) {
word[i] = c + 32;
i++;
continue;
}
i = 0;
if (isWordRedactWord(redactWords, word)) {
//write stars to file
char starStr[512];
for (int i = 0; i < getWordLen(word); i++) {
starStr[i] = '*';
}
fputs(starStr, outputFile);
}
else {
//write word to file
fputs(word, outputFile);
}
strcpy(word, emptyWord(word));
}
fclose(file);
fclose(outputFile);
}
In the initial while, I would only use while(!EOF).
Also, I believe you are using a lot more resources than necessary with the implementation of that for inside the while:
char starStr[512];
for (int i = 0; i < getWordLen(word); i++) {
starStr[i] = '*';
I suggest you to put it outside the while loop and see what happens.
If it is always giving you 188 of lenght, it is counting something that's constant, and may be related to that outer loop.
Hope you can solve it!
I wrote a short code that should copy the content of a file into an initialized array of strings and then print that array. I get no errors/warnings but still the program doesn't print anything when i run it. The code is the following:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int dim = 30;
int main() {
char* arr[dim];
int i = 0;
FILE* fp;
fp = fopen("test.txt", "r+");
if(fp == NULL) {
printf("\nError, breaking...");
return 0;
}
while(i <= dim) {
arr[i] = (char *)malloc(dim*sizeof(char *));
++i;
}
i = 0;
while(fscanf(fp, "%s", arr[i]) != EOF) {
printf("%s: added\n", arr[i]);
++i;
}
}
The file contains a series of words separated only by whitespaces and newline characters.
correct followings and see if it helps
1) initialise i to 0 ie. i=0;
2)
while(i < dim) { } // it should be < as array start with 0
If I have an options file along the lines of this:
size = 4
data = 1100010100110010
And I have a 2d size * size array that I want to populate the values in data into, what's the best way of doing it?
To clarify, for the example I have I'd want an array like this:
int[4][4] array = {{1,1,0,0}, {0,1,0,1}, {0,0,1,1}, {0,0,1,0}}. (Not real code but you get the idea).
Size can be really be any number though.
I'm thinking I'd have to read in the size, maloc an array and then maybe read in a string full of data then loop through each char in the data, cast it to an int and stick it in the appropriate index? But I really have no idea how to go about it, have been searching for a while with no luck.
Any help would be cool! :)
int process_file(int **array, char const *file_name)
{
int size = 0;
FILE *file = fopen(file_name, "rt");
if(fp == null)
return -1;//can't open file
char line[1024]; //1024 just for example
if(fgets(line, 1024, file) != 0)
{
if(strncmp(line, "size = ", 7) != 0)
{
fcloes(file);
return -2; //incorrect format
}
size = atoi(line + 7);
array = new int * [size];
for(int i = 0; i < size; ++i)
array[i] = new int [size];
}
else
{
fclose(file);
return -2;//incorrect format
}
if(fgets(line, 1024, file) != 0)
{
if(strncmp(line, "data = ", 7) != 0)
{
fcloes(file);
for(int i = 0; i < size; ++i)
delete [] array[i];
delete [] array;
return -2; //incorrect format
}
for(int i = 7; line[i] != '\n' || line[i] != '\0'; ++i)
array[(i - 7) / size][(i - 7) % size] = line[i] - '0';
}
else
{
fclose(file);
for(int i = 0; i < size; ++i)
delete [] array[i];
delete [] array;
return -2; //incorrect format
}
return 0;
}
Don't forget delete array before program ends;
Loops.
FILE *fp = fopen("waaa.txt", "r");
if(fp == null) { /* bleh */ return; }
int j = 0;
while(char ch = fgetc(fp)) {
for(int i = 0; i < 4; ++i) {
array[j][i] = ch;
}
++j;
}
I am not sure with the fgetc() syntax.. Just check on it. It reads one character at a time.