I've been trying to do file excercises in C but when I run programs from the professor or tutorials (which should work), the file always comes out blank. Is there a solution to this?
My computer is pretty old, but it can still run various programs, I don't undersand why it doesn't work with files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*program that prints on a file your shopping list*/
int main(int argc, char **argv)
{
FILE *fp= fopen("Shopping list.txt", "w");
int end= 0; //have you finished writing the articles
char article[80]; //the article you want to buy
int n; //quantity
while(!end){
printf("What do you need to buy? ");
fgets(article, 80, stdin);
article[strlen(article)- 1]= '\0';
fprintf(fp, "%s ", article);
printf("How much of it? ");
scanf("%d", &n);
fprintf(fp, "%d\n", n);
printf("Are you done? (1= Yes, 0= No) ");
scanf("%d%*c", &end);
}
fclose(fp);
}
It should print out on the file (which is created and remains empty) your input. There is no error message
I've resolved the problem by simply specifying the absolute path in fopen
Related
i'm new here and i'm trying to solve a FILE problem in c. Basically i have to create a program that lets the user input how many lines he wants to write in a file, create a new file, write those lines and the reading it and establish how many lines where written and print the number of lines.
int main() {
int x, lc=0;
char str[100];
FILE *fp=fopen("test.txt","w");
if (fp==NULL) {
printf("\nOpening failed");
}else{
printf("\nOpened correctly");
}
printf("\nStrings to write:\n");
scanf("%d",&x);
for (int i = 0; i < x; i++) {
fgets(str, sizeof str, stdin);
fputs(str,fp);
}
fclose(fp);
FILE *fr=fopen("test.txt", "r");
while (fgets(str, 100, fr)!=NULL) {
lc++;
}
fclose(fr);
printf("\nThere are %d lines",lc);
return 0;
}
If i leave the code like this it messes up with my for cycle and it only lets me write 3 lines because it does put a free line at the start of the file. Can you explain how do i solve that? or if it's just how fgets and fputs behave and i have to remember that blank line at the start. Thank you in advance. (i'll leave a file output as follows with numbers for the lines)
1)
2)it seems to work
3)dhdhdh dhdh
4)random things
As you can tell from the comments, there are a lot of ways to approach this task. The usage of "scanf" and "fgets" can get complex especially if mixed within the same reading task. But, just to give you one option as to deriving a solution, following is a snippet of code to offer one of many possible routes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int x, lc=0;
char str[101];
FILE *fp=fopen("test.txt","w");
if (fp==NULL)
{
printf("Opening failed\n");
}
else
{
printf("Opened correctly\n");
}
printf("Strings to write: ");
scanf("%d",&x);
for (int i = 0; i < x; i++)
{
printf("Enter string: ");
scanf("%s", str);
fprintf(fp, "%s\n", str);
}
fclose(fp);
FILE *fr=fopen("test.txt", "r");
while (fgets(str, 100, fr)!=NULL)
{
lc++;
}
fclose(fr);
printf("\nThere are %d lines\n",lc);
return 0;
}
You will note that both "scanf" and "fgets" are being used in this example, but not in reference to the same file. For user input, "scanf" is getting used. Once the file is closed and then reopened for reading, "fgets" is being used for that portion of the task.
Testing this program snippet out resulted in matching up the same quantity of lines read from the file as were entered.
#Una:~/C_Programs/Console/FileWrite/bin/Release$ ./FileWrite
Opened correctly
Strings to write: 4
Enter string: Welcome
Enter string: to
Enter string: Stack
Enter string: Overflow
There are 4 lines
Give it a try and see if it meets the spirit of your project.
I am trying to make a mad libs game but every time I test the first two scanf functions work, but the fgets function is not working how it should be.
it keeps printing the same things I want it to but it's showing the mad libs text before the user has a chance to type in the fgets input
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char color[20];
char COL2[20];
printf("Enter a color: ");
scanf("%s", color);
printf("Enter a another color: ");
scanf("%s", COL2);
printf("Enter a celebrity name: \n");
char celebrity[15];
fgets(celebrity, 15, stdin);
printf("Roses are %s", color);
printf("Violets are %s", COL2);
return 0;
}
fscanf(stdin, "%15s", celebrity);
It worked on my laptop, so it should be fine.
Also fscanf is better and safer usually from what I've heard. Because it checks for end of line.
I'm trying to write a program that asks for input of the name of a file and a char to be counted inside the file. But whenever I input the proper name of a file (like, "file.txt") it jumps right to the end of the program with a output like this:
"Name of the file: file.txt
Character to be counted:
The char occurs 0 times in the file "
...but I couldn't even type the char to be counted.
I know it's not an issue with the name of the file, because if I put the wrong name, it goes for the output I programmed for.
Would anyone care to explain me what's happening?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char filename[128];
printf("Name of the file: ");
scanf("%s", filename);
FILE * test = fopen(filename, "r");
if (test == NULL) {
printf("Error!!\n");
exit(1);
}
char inpt;
printf("Character to be counted:\n");
scanf("%c", &inpt); //gets the character to be counted
int count = 0;
char search = fgetc(test);
while(search != EOF) {
if (search == inpt) count++;
search = fgetc(test);
}
printf("The char occurs %d times in the file\n", count);
return 0;
}
I have this struct which I stack with information, I'm doing that via pointers, after doing that I'm saving all the info into a file named person.txt and then read it in the same program.
The problems I'm having are:
I can display the final result correctly, but, the person.txt file doesn't have any meaningful text in it, it's just a bunch of unknown symbols for me (I guess it's automatically saved in bit, but I don't know why it does that).
After inserting the info and the .txt file is created and the result is displayed, after closing the program, when trying to use only the code to read the file (meaning I make a commentary out of the code I won't need), it displays me something else, totally different of what I initially introduced.
Heres the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct eu
{
char name[30];
int age,weight, number;
};
int main()
{
struct eu *Eu1, euptr;
Eu1 = &euptr;
printf("Name & Surname:");
scanf("%[^\n]", Eu1->name);
printf("\n");
printf("Age:");
scanf("%d", &Eu1->age);
printf("\n");
printf("Weigt(kg):");
scanf("%d", &Eu1->weight);
printf("\n");
printf("Telephone number:");
scanf("%d", &Eu1->number);
printf("\n\nDisplay: ");
FILE *outinfo;
outinfo = fopen("person.txt", "a");
if (outinfo == NULL)
{
fprintf(stderr, "\nSomething went wrong!\n");
}
fwrite(&Eu1, sizeof(struct eu),1,outinfo);
fclose(outinfo);
outinfo = fopen("person.txt", "r");
while(fread(&Eu1, sizeof(struct eu), 1, outinfo))
printf ("\n Name:%s\n Age:%d\n Weight:%d\n Tel. Number:%d\n ", Eu1->name, Eu1->age, Eu1->weight, Eu1->number );
fclose (outinfo);
return 0;
}
you've messed the pointers. It is ok to save whole struct into the file, here is correctly working code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct eu
{
char name[30];
int age,weight, number;
};
int main()
{
struct eu eu1, *euptr;
euptr = &eu1;
printf("Name & Surname:");
scanf("%[^\n]", euptr->name);
printf("\n");
printf("Age:");
scanf("%d", &euptr->age);
printf("\n");
printf("Weigt(kg):");
scanf("%d", &euptr->weight);
printf("\n");
printf("Telephone number:");
scanf("%d", &euptr->number);
printf("\n\nDisplay: ");
FILE *outinfo;
outinfo = fopen("person.txt", "a");
if (outinfo == NULL)
{
fprintf(stderr, "\nSomething went wrong!\n");
}
fwrite(euptr, sizeof(struct eu), 1, outinfo);
fclose(outinfo);
outinfo = fopen("person.txt", "r");
while(fread(&eu1, sizeof(struct eu), 1, outinfo))
printf ("\n Name:%s\n Age:%d\n Weight:%d\n Tel. Number:%d\n ", euptr->name, euptr->age, euptr->weight, euptr->number );
fclose (outinfo);
return 0;
}
I've swapped eu1 & euptr variables for more logical naming.
fwrite will write the raw data you have given it, for the number of bytes you have specified. You are able to write this struct and read it back which is exactly what you are trying to do, all is working as intended. "Normal" text files with words use a particular encoding (usually ascii or unicode), and you need to use that encoding if you want it to be readable.
If you wanted this to be human readable, you would need to specify a format. A very simple one would be CSV (comma separated values).
So you might do something like
fprintf(outinfo, "%s,%d,%d,%d", Eu1.name, Eu1.age, Eu1.weight, Eu1.number);
//...
fseek(outinfo,0,SEEK_SET);// move to beginning of file
fscanf(outinfo, "%s,%d,%d,%d", &Eu2.name, &Eu2.age, &Eu2.weight, &Eu2.number);
this doesn't involve any error checking, and would need to be changed if your struct format changed, but would allow the file to be human readable if that was a requirement for you.
i don't know what to do, i'am try 10 minutes to fix it, but nothing.
i'am a beginner... so... don't dislike somfthing.
Please help me with code :D
i working with C one month and it is cool language.
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *pf;
char input[500];
char ime[500];
int ime2;
for(ime2 = 0; ime2 >= 9999999; ime2++);
/*
ime2 is: 1,2,3,4,5,6,7,8,9,10,11,12... 9999999
*/
pf = fopen("bruteforce.txt","w");
if(pf == NULL){
printf("Datoteka (.txt) po imenu: bruteforce nije pronadjena!\n");
} else {
printf("Sledeci text ce biti unesen: \n");
gets(input);
ime = input + ime2; // error
fputs(ime, pf);
printf("Uspesno je ispisano.\n");
fclose(pf);
}
system("pause >nul");
return 0;
}
First of all change
gets(input)`
to
fgets(input, sizeof(input), stdin)
and then try this
snprintf(ime, sizeof(ime), "%s %d", input, ime2);
BTW: maybe MSVC will not like snprintf() as is, they just add an underscore like this
_snprintf(ime, sizeof(ime), "%s %d", input, ime2);