#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define KEYSIZE 16
int main(){
FILE *fptr;
fptr = fopen("program.txt", "w");
if (fptr == NULL) {
printf("Error!");
exit(1);
}
int num_keys = 1524006529 - 1523999329;
const char *keys[num_keys];
int idx = 0;
int j;
for (j = 1523999329; j<= 1524006529; j++){
int i;
char key[KEYSIZE];
srand (j);
for (i = 0; i< KEYSIZE; i++){
key[i] = rand()%256;
printf("%.2x", (unsigned char)key[i]);
fprintf(fptr, "%.2x", (unsigned char)key[i]);
}
fprintf(fptr, "%s", "\n");
printf("\n");
keys[idx] = key;
idx ++;
}
fclose(fptr);
return 0;
}
I want to create a text file (program.txt) that has a new string key variable on each line. The code above produces a txt file that has this content though:
h?G??+?
Mp???+??
a?G????#*Jb`e0?H?????G??????
S6&}?Μ??G??OFf??|???s8f?G????ɼ???5q??(???K?G?? ??J????9?+???G?????cZ?n0?Jr?G?????#O9]??????jH??G???krT?̇)?A
?g?v?G??????h^(?^?
û/?G???^vB?~W?39???G??\????"?iJ_????G??p??>??????I???U?G????????:??o?RV???G??/?C?^?????|?۬?G??q#0.L$+??Nv?????G????u? L?Ϩ????dv?G??[??C「<??i???G??UyM??Ƭ/??CxXUě?G?????φ
:W&?G????=J???o???I??G???G%DA+"?~5?Z¡?G??øL#??O=e?b?u.O?G????*jjɥ+T?)?օ?G??2w }a,ȃ??tC?s?G??J"/c??Mn???f?y??G????p?Ay?c????~"<?G?????Ӆ?=%??#???G??o?\??<w???4WϚBE?G??}oh??(??#HK???&?G????Vfz
???e?s?G??/?Ye??iV?؛?,-te?G??I?Ҁx??5?B!b?4?G??6Z??9?P?GcB?}???Y?~|"??dd??G??????=?ms?
??G?????^??[H??\:???G??2(J??A??????G???????o#?i?/???G??P?5?5?5X??
What am I doing wrong here?
Related
I'm trying to make a random input.txt file generator in C language but I keep getting random characters in the output file. I tried using both putw() and fwrite() but I get the same result.
for(int i = 0; i < 100; i++){
ran_numb = rand()%(ram_size-1);
// printf("%d", ran_numb);
// int err = putw(ran_numb, fp);
fwrite(&ran_numb, sizeof(int), 1, fp);
fputc('\n', fp);
}
output file:
~
ô
u
?
{
ø
È
Õ
å
×
¡
full code:
#include <stdio.h>
#include <string.h>
int main (int argc, char* argv){
generate_input_file(2048);
}
void generate_input_file(int ram_size){
int ran_numb = 0;
FILE *fp = fopen("input.txt", "w");;
if(fp == NULL){
printf("can't open file!");
}
for(int i = 0; i < 100; i++){
ran_numb = rand()%(ram_size-1);
// printf("%d", ran_numb);
// int err = putw(ran_numb, fp);
fwrite(&ran_numb, sizeof(int), 1, fp);
fputc('\n', fp);
}
}
After the help of the comments, this is the final program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include "simulator.h"
int main (int argc, char* argv){
generate_input_file(2048);
}
void Gen_Input_File(int ram_size){
int ran_numb = 0;
const char filename[] = "input.txt"
FILE *fp = fopen(filename, "w");;
if(fp == NULL){
fprintf(stderr, "Failed to open file '%s' for writing: %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
srand(time(NULL));
for(int i = 0; i < 10000; i++){
ran_numb = rand()%(ram_size-1);
fprintf(fp, "%d\n", ran_numb);
}
}
I've added the missing headers to stop warnings.
I am working on a function that reads from a file (fp) and stores in the words array. I declared MAX_WORD_SIZE as 128, but when I input any file into this function and check with valgrind, it tells me I have an uninitialized value in the line "while(getline(&line,&count,fp)!=-1)" I really don't get it: what is the uninitialised value? My fp file is valid and the word array is also declared well. Thank you in advance.
#include "functions.h"
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int read_file(FILE *fp, char words[][MAX_WORD_SIZE + 1], int size) {
int i=0;int j=0;
size_t count=MAX_WORD_SIZE;
char* line=malloc(MAX_WORD_SIZE);
while(getline(&line,&count,fp)!=-1){
for(;count>0;count--,j++){
sscanf(line,"%c",&words[i][j]);
}
i++;
}
int totalNums = i;
int totalNum = j;
if (i<size){
return 1;
}
fclose(fp);
return 0;
}
This is the function that I called this read_file function:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.h"
int main(int argc, char *argv[]) {
const char* fileName = argv[1];
FILE *fp = fopen(fileName, "r");
if (fp == NULL) {
printf("Invalid input file\n");
return 1;
}
int size = 0;
int validity = fscanf(fp, "%d", &size);
int returnValue = 0;
char words[size][MAX_WORD_SIZE + 1];
if(validity != 1 || size <= 0) {
printf("The first line is not a valid number\n");
return 1;
}
returnValue = read_file(fp, words, size);
if (returnValue == 1) {
fclose(fp);
return 1;
}
return 0;
}
I am trying to read numbers from multiple text files starting with 'numbers' and calculate the sum. I am getting some random numbers not contained in the file I am opening. I have tried initializing the array at 0 but that just made everything 0 on output.
This is the problem section I believe
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
DIR *d;
struct dirent *dir;
int fileAndSum(){
int sum = 0, i = 0;
int nums[100];
FILE* fptr = fopen(dir->d_name,"r");
fputs("11111111111111111", fptr);
for(i = 0; i <10; i++){
fscanf(fptr,"%d", &nums[i]);
printf("%d\n", nums[i]);
sum+=nums[i];
}
printf("%s\n", "----------sum--------------");
printf("%d\n", sum);
fclose(fptr);
The rest
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
DIR *d;
struct dirent *dir;
int fileAndSum(){
int sum = 0, i = 0;
int nums[100];
FILE* fptr = fopen(dir->d_name,"r");
for(i = 0; i <10; i++){
fscanf(fptr,"%d", &nums[i]);
printf("%d\n", nums[i]);
sum+=nums[i];
}
printf("%s\n", "----------sum--------------");
printf("%d\n", sum);
fclose(fptr);
}
int main(void) {
d = opendir("numdir");
char strhold[50] = "numbers";
char fileName[50];
while ((dir = readdir(d)) != NULL) {
printf("%s\n", dir->d_name);
if(strstr(dir->d_name, strhold)){
printf("%s%s%s\n", "----------Now reading ",dir->d_name,"--------------");
fileAndSum();
printf("%s\n", "----------Next file--------------");
}
}
closedir(d);
return(0);
}
Your files to read are in directory numdir, but you are trying to read files in current working directory.
Also you should check if file open is successful.
to fix, the part
FILE* fptr = fopen(dir->d_name,"r");
should be
char fileName[1024];
snprintf(fileName, sizeof(fileName), "numdir/%s", dir->d_name);
FILE* fptr = fopen(fileName,"r");
if(fptr == NULL){
puts("open failed");
return 0;
}
I'm trying to run this code on Eclipse. But there is an error about the header mhash.h
"Error: No such a file or directory"
The code is this:
#include <mhash.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
MHASH td;
unsigned char buffer;
unsigned char *hash;
td = mhash_init(MHASH_MD5);
if (td == MHASH_FAILED) exit(1);
while (fread(&buffer, 1, 1, stdin) == 1) {
mhash(td, &buffer, 1);
}
hash = mhash_end(td);
printf("Hash:");
for (i = 0; i < mhash_get_block_size(MHASH_MD5); i++) {
printf("%.2x", hash[i]);
}
printf("\n");
exit(0);
}
You need to install mhash first. Get it from http://sourceforge.net/projects/mhash
I get a bad access error in the middle of the for loop, always when i=4. Does anybody know the reason for this? It works until i=4, but I don't see why I wouldn't get the bad access error in any other part of the for loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXF 51
#define MAXFILE 200
int recommend(int fid, char *funcs[]){
int i;
for(i=0; i<fid; i++)
*funcs++;
printf("\nRecommended Function: %s\n", *funcs);
return 0;
}
int overlap(char *list[], char name[], int n){
int over=0, fid=202, i, j, k, m;
for(i=0; i<n; i++){
m=strlen(*list);
int lap=0;
for(j=0; j<(strlen(name)-1); j++){
for(k=0; k<m; k++)
if(list[i][k]==name[j]){
lap+=1;
break;
}
}
if(over<lap){
over=lap;
fid=i;
}
*list++;
}
return fid;
}
int readfile(char *flist[], FILE *fptr){
char a[MAXF];
int size=0;
while(fscanf(fptr, "%s\n", a) != EOF){
flist[size]=malloc(sizeof(char)*(1+strlen(a)));
strcpy(flist[size++],a);
}
return size;
}
int main () {
int n, id;
char fnname[MAXF], filename[MAXF], *flist[MAXFILE];
FILE *fp;
printf("Name of network file: ");
gets(filename);
printf("\nFunction Name: ");
gets(fnname);
fp=fopen(filename, "r");
if(fp==NULL)
printf("\nCould not open file.\n");
else {
n=readfile(flist, fp);
id=overlap(flist, fnname, n);
recommend(id, flist);
}
return 0;
}
It looks to me as if this:
m=strlen(*list);
should be:
m=strlen(list[i]);
And this:
*list++;
should not be there at all.