Im having a problem finding out where to store my Text File which I want to read from. I stored it in the same directory as the executable file but it is still not reading the file in. After much digging and searching I have found that the problem has something to do with the "Derived Data" in Xcode. When I look for the source file , by right clicking on the main.c file and locating the file in finder I find that it is located within the Derived Data. Whenever I run this program it creates some derived data (which Xcode is supposed to do) but my .c file is stored within this derived data and when I try and store the text file in the same location the executable is located (the same place the .c file is located) I am not able to read in the file.
Suggestions?
Ideas?
Help..
#include <stdio.h>
#include <unistd.h>
extern int errno;
int main (void){
int term;
long long StudentID;
char lastname [16];
char firstname[16];
char subject[4];
int catalog;
char section[4];
char filename [9];
FILE *cfPtr;
printf("Enter file name: ");
scanf("%s",filename);
/*
char cwd [1024];
if (getcwd(cwd, sizeof(cwd)) != NULL)
fprintf(stdout, "Current working dir: %s\n", cwd);
*/
system("pwd");
if((cfPtr=fopen(filename,"r"))==0){
//printf("%s jlk",strerror(errno));
printf("File could not be opened\n");
}
else{
printf("%-35s%-10s%-10s%-10s%-10s\n","Last Name, First Name","Term","ID","Course","Section");
int i;
for(i=0; i<75; i++){
printf("-");
}
fscanf(cfPtr,"%d%lld%s%s%s%d%s",&term,&StudentID,lastname,firstname,subject,&catalog,section);
while(!feof(cfPtr)){
printf("%-35s%-10s%d%lld%-10s%-10s\n", lastname, firstname,term,StudentID,subject,section);
fscanf(cfPtr,"%d%lld%s%s%s%d%s",&term,&StudentID,lastname,firstname,subject,&catalog,section);
}
fclose(cfPtr);
}
return 0;
}
If you've set up a "Command Line Tool" project in Xcode, then you can set the working directory as follows. First click on the project name at the top of the Xcode window and select "Edit Scheme..."
Then select "Run Debug" on the left and change the "Working Directory" on the right.
The alternative is to download the command line tools (Preferences/Downloads). Then you can open a terminal window, and compile/run from the command line.
Related
I don't understand why my script below seems to work without creating any files.
script.c:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]){
printf("P_tmpdir is '%s'\n", P_tmpdir);
FILE *tmp = tmpfile();
if(tmp == NULL){
printf("Unable to create temp file");
exit(1);
}
else{
printf("Temporary file is created\n");
}
for(int i = 0; string[i] != '\0'){
fputc(string[i], tmp);
}
rewind(tmp);
while(!feof(tmp)){
putchar(fgetc(tmp));
}
sleep(3);
return(0);
}
The P_tmpdir variable returns me the "/tmp" directory although in the sleeping time no new file is created in it... can you help me or explain me plz ?
Quoting cppreference.com (emphasis mine):
On some implementations (e.g. Linux), this function actually creates, opens, and immediately deletes the file from the file system: as long as an open file descriptor to a deleted file is held by a program, the file exists, but since it was deleted, its name does not appear in any directory, so that no other process can open it.
The file does not have to be "visible" in the file system tree, as long as a process has a handle on it, the file continues to exist.
If you want a file that's visible in the file system tree you should use mkstemp.
I've seen a lot of tutorials online about how you can edit files with c, how to write data within a text file and similar things.Like this code:
#include <stdio.h>
#include <stdlib.h> /* For exit() function */
int main()
{
char sentence[1000];
FILE *fptr;
fptr = fopen("program.txt", "w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
gets(sentence);
fprintf(fptr,"%s", sentence);
fclose(fptr);
return 0;
}
What I want to do is to make a folder, in VS there are four folders for use I.E
External Dependencies,
Header files,
Resource files,
Source files;
I'd like to make it possible to create another folder and within it store text files or another directories.
And after then I can delete the file or directory. Like that I can create the file within the code without any need to do that outside the programme.
I fairly new to the C programming language. I am writing a program that will eventually read through mp3 files from a user inputted directory, and sort the mp3's into artist/album folders by utilizing the metadata in the id3's. I am accessing the user's directory using the system() function call, and generating a .txt file containing all of the mp3's in that directory. However I am running into problems when trying to access the first mp3 file. I am building the mp3 file's path, but the file will not open. The file DOES open when I hard code the path.
#include <stdio.h>
#include <string.h>
struct FILE_head
{
char file_id[3];
char version[2];
char flags;
char size[4];
};
int main()
{
//declare vars
char cd[1200];
char mp3[200];
char dir[1000];
char mp3_path[1200];
FILE *list_file;
FILE *mp3_file;
struct FILE_head id3;
char dir_cmd[1300] = "dir ";
char find_cmd[100] = "/b | find \".mp3\" > \"mp3List.txt\"";
int dir_len;
int amt_read;
//main code
while(1)
{
cd[0]='c';
cd[1]='d';
cd[2]=' ';
cd[3]='\0';
printf("Enter the directory where mp3's are located:");
scanf("%s", dir);
strcat(cd, dir);
if(system(cd) == 0) //if directory is valid, break. otherwise stay in loop/reprompt
break;
printf("Valid directory Ex--> c:\\users\\username\\music\n");
}
//build cmd statement
strcat(dir_cmd, dir);
strcat(dir_cmd, find_cmd);
system(dir_cmd);
dir_len = strlen(dir);
strcpy(mp3_path, dir);
printf("%s\n", mp3_path);
list_file = fopen("mp3List.txt", "rb");
if(list_file != NULL)
{
while(fgets(mp3, sizeof(mp3), list_file))
{
printf("%s", mp3);
strcat(mp3_path, mp3);
printf("%s\n", mp3_path);
mp3_path[strlen(mp3_path)-1] = '\0';
mp3_file = fopen(mp3_path, "rb");
if(mp3_file != NULL)
{
printf("in this loop");
fread(&id3, sizeof(id3), 1, mp3_file);
printf("%s\n", id3.file_id);
}
}
}
return 0;
}
This is my first time posting, so if any more information would be helpful please let me know. I realize there might be "better" ways to access directories, but I don't want to use any functions that are not in the C std lib.
This is my output:
C:\Users\Mitchell\Projects>mp3sort.exe Enter the directory where mp3's
are located:c:\users\mitchell\projects\music_test\
c:\users\mitchell\projects\music_test\ 01 - Time to Pretend.mp3
c:\users\mitchell\projects\music_test\01 - Time to Pretend.mp3
01-all_that_remains-this_calling.mp3
01-all_that_remains-this_calling.mp3t\01 - Time to Pretend.mp3
07 Billy Joel - Everybody Loves You Now.mp3 07 Billy Joel - Everybody
Loves You Now.mp3Time to Pretend.mp3
Tears for the Sheep.mp3 Tears for the Sheep.mp3dy Loves You
Now.mp3Time to Pretend.mp3
C:\Users\Mitchell\Projects>
I know that my current build for the all the files after the first isn't correct, but I was just focused on getting the first mp3_path to work. I removed the newline by: mp3_path[strlen(mp3_path)-1] = '\0';
system() executes provided command in subprocess. When you do system ("cd somedir"), cd somedir is executed in child process, thus working directory of your process remains unaltered.
If you want to change your process working directory, use chdir() (or _chdir(), alternatively SetCurrentDirectory if you want to use Windows API) function.
Alternatively, you can avoid changing working directory by prepending directory to file names.
I am using fopen(3) in C to read file and process it. The file is present in current working directory where the binary exists, but I am unable to read the file (Linux environment / Cygwin environment).
Here is the sample code:
C code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
FILE *inFile;
static char fileName[255];
int process_file(FILE *inFile)
{
char ch;
inFile = fopen(fileName,"r");
if (inFile == NULL)
{
perror(fileName);
exit(1);
}
else
{
// Process file
}
fclose(inFile);
return 0;
}
int main(int argc, char *argv[])
{
printf("Enter filename to process \n");
scanf("%s", fileName);
process_file(inFile);
getchar();
return 0;
}
I have file permissions set to 777 in the current directory. The resulting binary as well as my source code reside in this directory where the input file exits. Why is the file not opened?
Update :
This question was written in few years back and this code could be improved a lot.
1. The process file should accept char * or char array instead of file pointer
2. unused variables can be removed
3. unused libraries or include files can be removed
4. Can make use of argv to accept filename with path from cmdline
5. return instead of exit in process_file and also proper return code instead of returning 0 from process_file.
I should have asked this question little more elaborate...
I had three functions to process the same file, like process_fil1e1(), process_file2() and process_file3() even though I called fclose() in all three functions. Somehow the file handle was not closed that properly or the file pointer pointed to EOF or some undefined behavior. It was not working fine.
When I used a single process file and rewind() together, it worked fine...
Be sure to input file name with its extension. This may cause problems with reading the file.
If you know the extension of the file you can input only the name and after that make the program add the extension. After scanf("%s", fileName); add strcat(fileName, ".txt"); if you want to enter only the name without extension and the file you read has extension .txt.
Your inFile and fileName variables are extern so you don't need to have arguments for the function process_file();, any function can access those variables.
You can change function int process_file(); to void process_file(); and delete return 0, you don't need that.
You have declared the inFile and fileName as global. You should change your function prototype from
int process_file(FILE *inFile)
to
int process_file()
This would at least make your program more clear. Now regarding your problem: It would almost certain be that you are doing something wrong in the input file (like not putting in the file extension) in your input. Remember, you need to pass the complete file name (including the extension which on some systems like Windows (by default) would be hidden). Otherwise, the logic looks correct to me, and it should work fine.
The title doesn't really do this topic justice. It's actually quite simple, my problem that is. I have a program (code below) written in the C language. I want this program to create an exe file that can be ran through the command prompt console window and that will also take a text file as a parameter. So, long story short; I need it to say this on the command line in CMD:
C:\Users\Username\Desktop\wrapfile.exe content.txt
My only problem is getting the code right. I want to tell Visual Studio: "The file you should open is given in the command prompt window as a parameter, there is no set location..."
How do I do that?
Here is my working code (Although you will have to change a few things in the *fp definition.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp; // declaring variable
fp = fopen("c:\\users\\*Put you're PC username here*\\Desktop\\contents.txt", "rb"); // opens the file
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");
}
}
Thanks everyone!
As Ken said above, the arguments of the main method are the values that you pass in from the command line. Argc is 'argument count' and argv is 'argument values'. So to open the fist argument passed in from the command line, change
fp = fopen("c:\\users\\*Put you're PC username here*\\Desktop\\contents.txt", "rb"); // opens the file
to
fp = fopen(argv[1],"rb");
Just make sure to do error checking (ie argv[1] is not null) before you try to fopen the input. Also FYI, in your case argv[0] will be the name of your executable.