Okay so my problem is an assertion failure. What I don't understand is that my program correctly inputs from the file, to the array, then prints to the screen, but still shows this error and I just can't figure it out. There's going to be more to this program so please disregard the unused functions.
My code is as follows:
#include <stdio.h>
#include <stdlib.h>
#define MAX 272
FILE* csis;
void processFile(char line[]);
int cipher();
int main(void) {
char line[MAX];
processFile(line, MAX);
fclose(csis);
return (0);
}
void processFile(char line[]) {
FILE* fp;
int i = 0;
if (!(fp = fopen("congress.txt", "r"))) {
printf("File could not be opened for input.\n");
exit(1);
}
fseek(fp, 0, SEEK_END);
fseek(fp, 0, SEEK_SET);
for (i = 0; i < MAX; ++i) {
fscanf(fp, "%c", &line[i]);
printf("%c", line[i]);
}
fclose(fp);
}
int cipher() {}
It looks like you are closing an unopened file handle, in the future you can initialize your file handles to NULL and then test before closing.
FILE *csis = NULL;
...
if (csis)
fclose(csis);
i think you mean
fscanf(fp, "%s", &line[i]);
Related
Program asks for input and stores it in a variable, then confirms the operation printing the content of the file. Or at least it had to, when the program ends it doesn't print the file content, I can't seem to find an answer, I've been looking in the docs but can't really figure it out.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE * file1 = fopen(".out", "w+");
char *s = malloc(513);
fgets(s, 513, stdin);
if (fprintf(file1, "%s", s) < 0)
{
printf("Something failed while writing to the file\n");
return 1;
}
else
{
char *t = malloc(513);
fread(t, sizeof(char), 1, file1);
printf("Success! Input was: %s \n", t);
return 0;
}
}
P.S: Very new to C, though it may seem obvious for you I have no clue whatsoever.
There are 2 issues here,
1 - you wrote to the file handler and you are trying to read from that point onwards - you didnt rewind the file pointer!
2 - you are just reading 1 character and not the amount you wrote to it!
#include <string.h>
...
int n = strlen(s);
rewind(file1); // rewind before read
fread(t, sizeof(char), n, file1); // read as much as you wrote
Some problems in your code:
You are not checking the return value of fopen(), malloc(), fgets() and fread().
You are writing one character to the output stream, without rewinding it.
Here's how your code should look like:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE * file1 = fopen(".out", "w+");
if (!file1) {
printf("Could not open file.\n");
return 1;
}
const size_t n = 513; // Use constants, not litterals.
char *s = malloc(sizeof(char) * n);
if (!s) {
printf("Internal error.\n");
fclose(file1);
return 1;
}
if (!fgets(s, n, stdin)) {
printf("Input failed.\n");
fclose(file1);
return 1;
}
if (fprintf(file1, "%s", s) < 0) {
printf("Something failed while writing to the file\n");
fclose(file1);
return 1;
}
char *t = malloc(sizeof(char) * n);
if (!t) {
printf("Internal error.\n");
fclose(file1);
return 1;
}
rewind(file1);
int ret = fread(t, sizeof(char), n, file1); // Read n characters, not 1.
if (ret != strlen(s)) {
if (feof(file1)) {
printf("Error reading .out: unexpected end of file.\n");
} else if (ferror(file1)) {
perror("Error reading .out");
}
fclose(file1);
return 1;
}
printf("Success! Input was: %s \n", t);
}
1) I'm trying to open a file, read the mix data (ints, chars and strings) and store them into args.
1.1) so in the sample.txt is a total of 13 (excluding args[0])
2) Need to read a file from terminal "./myprog.c < sample.txt"
Heres my code and have no idea where i went wrong:
sample.txt:
123 213 110 90 1
hello my friend
boo bleh
a b c
myprog.c:
#include <stdio.h>
int main()
{
int i = 1;
FILE *fstin=fopen(argv[0], "r"); //open the file
if (fstin == NULL) {
puts("Couldn't fopen...");
return -1;
}
//Getting all the inputs from file
while ((fscanf(fstin, "%d", argv[i])) != EOF){
i++;
}
fclose(fstin);
for (i=0; i<10; i++) {
printf("%d\n",argv[i]);
}
return 0;
}
Any help is greatly appreciated!
PS: Would like if anyone could post their complete solution? Will upload unto this post and let everyone have a review of this problem
PPS: Please excuse the poor level of coding as I am a beginner and completely new to C.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int ac, char *av[]){
int i, argc=0;
char **argv=NULL, data[16];
FILE *fstin = stdin;
if(ac == 2){
if(NULL==(fstin = fopen(av[1], "r"))){
puts("Couldn't fopen...");
return -1;
}
}
while (1==fscanf(fstin, "%15s", data)){
argv = realloc(argv, (argc+1)*sizeof(char*));
argv[argc] = malloc(strlen(data)+1);
strcpy(argv[argc++], data);
}
if(ac == 2)
fclose(fstin);
for (i=0; i<argc; ++i) {
printf("%s\n", argv[i]);
}
//deallocate
return 0;
}
You are making mistake at 2nd point where you divert your file to other file which is wrong. Actually you need to first compile and need to make executable.
gcc -o my_prog ./myprog.c -Wall
You need to execute this program as below to read file from c program:
./my_prog ./sample.txt
As you are new to C programming first go to man pages related to file operations.
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
//If command line argument is not inserted then stop operation
if (2 != argc) {
printf("Invalid number of arguments : %d\n", argc);
return -1;
}
int size = 0, ret = 0;
char *data = NULL;
FILE *fp = NULL;
//Open file in read mode given from command line argument
if (NULL != (fp = fopen(argv[1], "r")))
{
//Find size of file
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
//if file is empty no need to read it.
if (size > 0)
{
//Data pointer which contains file information
data = (char *) calloc(sizeof(char), size);
if (NULL != data)
{
//Read whole file in one statement
fread(data, sizeof(char), size, fp);
printf("File %s is readed successfully\n", argv[1]);
printf("Data:\n");
printf("%s\n", data);
free(data); data = NULL;
}
else
{
perror("memory allocation failed\n");
ret = -1;
}
}
else
{
printf("File %s is empty\n", argv[1]);
}
fclose(fp); fp = NULL;
}
else
{
perror("File open failed\n");
ret = -1;
}
return ret;
}
Now Test it on your setup and if any query please post comments.
I compile the code then run it but it returns "Error in writing encrypted data to file. So I assume the issue is somewhere in fwrite.
But I cannot pinpoint it.
I need some help here if you could explain the problem that would be very helpful thanks a lot
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int encrypt_data(FILE *);
int main(void)
{
FILE *fp;
int return_code;
printf("Please enter the file to be encrypted: ");
char filename[200];
scanf("%s", filename);
fp=fopen(filename,"r");
return_code = encrypt_data(fp);
return 0;
}
int encrypt_data(FILE *disk_fp)
{
int i;
unsigned long int file_size;
int key_length;
char *file_buff = NULL;
char key[] = "12`3-vk0fn";
key_length = strlen(key);
fseek(disk_fp, 0, SEEK_END);
file_size = ftell(disk_fp);
rewind(disk_fp);
file_buff = malloc(file_size);
if( fread(file_buff, file_size, 1, disk_fp) != 1)
{
printf("Error in reading file\n");
return -1;
}
for( i=0; i<file_size; i++)
{
file_buff[i] = file_buff[i] ^ key[i%key_length];
}
rewind(disk_fp);
if( fwrite(file_buff, file_size, 1, disk_fp) != 1)
{
printf("Error in writing encrypted data to file\n");
return -1;
}
free(file_buff);
fclose(disk_fp);
return 0;
}
You are opening file with "r" mode which means readonly. Then you try write something to it. By the way you don't check that you open file without errors and don't close it when fwrite/fread failed.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int encrypt_data(FILE *);
int main(void)
{
FILE *fp;
int return_code;
printf("Please enter the file to be encrypted: ");
char filename[200];
fgets(filename, 200, stdin);
fp=fopen("filename","w+");
return_code = encrypt_data(fp);
return 0;
}
int encrypt_data(FILE *disk_fp)
{
int i;
unsigned long int file_size;
int key_length;
char *file_buff = NULL;
char key[] = "12`3-vk0fn";
key_length = strlen(key);
fseek(disk_fp, 0, SEEK_END);
file_size = ftell(disk_fp);
rewind(disk_fp);
file_buff = malloc(file_size);
if( fread(file_buff, file_size, 1, disk_fp) != 1)
{
printf("Error in reading file\n");
return -1;
}
for( i=0; i<file_size; i++)
{
file_buff[i] = file_buff[i] ^ key[i%key_length];
}
rewind(disk_fp);
if( fwrite(file_buff, file_size, 1, disk_fp) != 1)
{
printf("Error in writing encrypted data to file\n");
return -1;
}
free(file_buff);
fclose(disk_fp);
return 0;
}
The file I am trying to encrypt is "encrypt.txt" it is just a sentence of nonsense but when I compile this code and then ./a.out it asks me for the file name i enter encrypt.txt I thought it might just want the name but either way it returns "Error in reading file".
I think my fgets() fopen() is the culprit but I am very very lost in how to fix it.
If you could find the error in the code and then explain why it was messing things up it would help me in the future. Thanks a lot.
Remove double quotes of the filename variable. And put it as
fp=fopen(filename,"w+");
maybe I am wrong but with close inspection I thought you better use
scanf("%s", filename);
Instead of the fgets() function. You are reading text from user and not from file at that moment after all.
I was wondering how I can get this code to overwrite a textfile from it's text value to it's ASCII value.
I want it to do something like this:
CMD > c:\users\username\desktop>cA5.exe content.txt
content.txt has "abc" in it and I want the command line to change the "abc" to it's ASCII values. 97... etc. I don't want anything written in the command window, I want it to change in the text file. Is this possible, if so, how could I do it with this existing 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");
}
}
Not the best code but very simple.
#include <stdio.h>
#include <stdlib.h>
void convertToAHex(char *data, long int size, FILE *file){
rewind(file);
int i;
for(i = 0; i < size; ++i){
fprintf(file, "%d ", data[i]);
}
}
int main(int argc, char *argv[]){
if(argc != 2){
return EXIT_FAILURE;
}
FILE *file = fopen(argv[1], "r+");
if(file){
char *data;
long int size;
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
data = (char *) calloc(size, sizeof(char));
if(data){
fread(data, 1, size, file);
convertToAHex(data, size, file);
free(data);
}
fclose(file);
}
return EXIT_SUCCESS;
}