How to read file in C programming using codeanywhere? [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
here is the code given to me and a have also a text file where i need to get the Text from and compile the program.

You need to use open a file with fopen() first. But the current user needs to have perms to read/write the file.
We will use r to only read from a file. If the file is not read it will return NULL. You can fscanf() function to get the value of a file. The second parameter represents the type of the variable as in this case it's a string(char), third param is the mem address of the variable itself. Kind of like file version of scanf().
int main()
{
char a[1000];
FILE *myFile;
if ((myFile = fopen("C:\\myUSER\\newprogram.txt","r")) == NULL){
printf("Error! opening file");
exit(1);
}
fscanf(myFile ,"%s", &a);
printf("Value of a=%s", a);
fclose(myFile);
return 0;
}

Related

Probelm with fscanf in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Ok, so i've been having a probelm with using int and char for my function. And is getting an error message enter image description here want to know how i should fix this with the code that i have:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char name[100];
int roll_no, chars;
float marks;
fp = fopen("records.txt", "r");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("Testing fscanf() function: \n\n");
printf("Name:\t\tRoll\t\tMarks\n");
while( fscanf(fp, "Name: %s\t\tRoll no: %d\t\tMarks: %f\n"
, name, &roll_no, &marks) != EOF )
{
printf("%s\t\t%d\t\t%.2f\n", name, roll_no ,marks);
}
fclose(fp);
return 0;
}
The intedend output was this... enter image description here
Advice/help on how to use the char function at line 8 would be appericated
NOTE: This answer refers to revision 1 of the question. Meanwhile, OP has clarified the question and is asking for additional help concerning a related issue. That is why this answer does not fully address the question in its current state.
That's not an error message, that is simply a warning because you declare the variable 'chars' on line 8 and never use it in your program. Your program should be able to run even if there are compiler warnings, which are different from compiler errors.

Where does the function fopen() in C creates the file, opened in write mode? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Where is the directory?
i've searched for possibly all the directories but couldn't find any file related to the program
In the current working directory. If it does not maybe the call is not successfully. Check its return-code.
fopen() function is used to open a file to perform operations such as reading, writing etc. In a C program, we declare a file pointer and use fopen() as below. fopen() function creates a new file if the mentioned file name does not exist. The directory where you run the program from
The following example shows the usage of fopen() function.
#include <stdio.h>
#include <stdlib.h>
int main () {
FILE * fp;
fp = fopen ("file.txt", "w+");
fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012);
fclose(fp);
return(0);
}
Let us compile and run the above program that will create a file file.txt with the following content −
We are in 2012
Now let us see the content of the above file using the following program −
#include <stdio.h>
int main () {
FILE *fp;
int c;
fp = fopen("file.txt","r");
while(1) {
c = fgetc(fp);
if( feof(fp) ) {
break ;
}
printf("%c", c);
}
fclose(fp);
return(0);
}
Let us compile and run the above program to produce the following result −
We are in 2012

file name contains spaces can't open with fopen [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm writing a program which counts how many times each C keyword is contained in an ASCII file selected by the user. So I use scanf to save the name that the user selected as filename and check with fopen if a file with that name exists in the same dir as the C program. The problem lies in the fact that if the filename selected by the user contains spaces fopen gives an error because it can't find such a file. So here's the question, how do I open a file with fopen that contains spaces in the name of it? Here is the code that i used for the program
selectfilename(filein,1) ;
if (fopen(filein,"r") == NULL){
perror("Error ") ;
return (1) ;
}
void selectfilename(char *cp, int num){
if (num == 1) printf("Please select the name of the file to be opened including the extension : ") ;
else printf("Please select the name of the file to save the statistics including the extension : ") ;
scanf("%s",cp++) ;
}
I think your problem is with scanf, not fopen – which handles filenames with spaces just fine.
scanf("%s") only parse until the first space. It's hard to propose a fix without seeing more of the code.
Update:
Since you read from stdin you can try this to read until the line terminator.
char buf[256];
int rv = scanf ("%255[^\n]", buf); // 255 is max chars to read
if (rv == 0 || rv == EOF)
buf[0] = 0;
printf ("[%s]\n", buf);
Update 2: Fixed bugs reported by #chux

C Language - Windows has triggered a breakpoint [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am coding C language in Visual Studio C++.
In the first calling of this function there is no problem. It makes all of process but in the second calling VS give "Windows has triggered a breakpoint" error.
However, I compile and run the code in Linux, there is no problem.
void printDataPagePersons(int pageNumber)
{
Person* pageofCity = (Person*)malloc(sizeof(Person)* RECORD_COUNT);
printf("page of city : %d\n",sizeof(*pageofCity));
FILE* fp;
fp=fopen("x.dat", "rb");
fseek(fp, PAGE_SIZE*pageNumber,SEEK_SET);
fread(pageofCity, PAGE_SIZE,1, fp);
fclose(fp);
//OTHER PRINTING PROCESSES...
}
Here you allocate a buffer:
Person* pageofCity = (Person*)malloc(sizeof(Person)* RECORD_COUNT);
This is the size of that buffer:
sizeof(Person)* RECORD_COUNT
You then read data from a file into that buffer at this line of code:
fread(pageofCity, PAGE_SIZE,1, fp);
There you are telling the fread function to read a PAGE_SIZE of bytes into that buffer.
I'm guessing the cause of your problems is this condition is also true:
PAGE_SIZE > sizeof(Person)* RECORD_COUNT

How to access a .txt file from command-line input in C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
For example, I run my program like:
program.exe < text.txt
I want the program to read from file text.txt. How do I begin doing this?
Ok, as this is a textfile, you probably want to read it line by line, so
char buf[1024];
while (fgets(buf, 1024, stdin))
{
/* do whatever you need with the line that's in buf here */
}
Note your code doesn't know about the file, it just reads from standard input. With <, you tell your environment (CMD on windows, a shell like e.g. bash on *nix) to open that file for you and provide it to your program as the standard input instead of the default, the controlling terminal, which would normally just read from the keyboard.
Hint: 1024 is kind of a random pick, most text files don't have lines exceeding 1kb. You might want to modify it to better suit your expected input.
another way to do what you are looking for is
#include <stdio.h>
int main (void) {
int c;
while ((c = fgetc(stdin)) != EOF) fputc(c, stdout);
return 0;
}
some more help here

Resources