int main(int argc, char* argv[])
{
FILE *pfd;//will get the file we are gone read from
char fileName[40];//get files name
char line[1024];
FILE *simulate;//will get the file we are gone read from
char line2[1024];
int arrSize=argc-2;
station * stations=( station * )malloc(sizeof(station)*(arrSize));//intilzing ther array
int i=2;//Auxiliary variable for the first for loop-reads information from all fiels
int j=0; //Auxiliary variable to help as clean the memory
ClientsLinkedList* data;
Link * temp;//temp varbale to help us clean the memory
Link * tempNext;
if(stations==NULL)
{
printf("Failed to allocate memory");
}
for(i;i<argc;i++)
{
data=CreateClientsLinkedList();
stations[i].m_clients=*data;
strcpy(fileName,argv[i]);
///* Open the file. If NULL is returned there was an error */
if((pfd = fopen("station.txt" , "r")) == NULL)
{
printf("Error Opening File.\n");
}
while( fgets(line, sizeof(line), pfd) != NULL )
{
ReadByCharName(line,stations,i);
}
fclose(pfd); /* Close the file */
}
////************************************************reading from simulation file*******************************************
/*** Open the file. If NULL is returned there was an error */
if((simulate = fopen("simulation.txt", "r")) == NULL)
{
printf("Error Opening File.\n");
}
while( fgets(line, sizeof(line2), simulate) != NULL )
{
ReadSimulation( line2,arrSize,stations);
}
fclose(simulate); /* Close the file */
////*********************************************clening memory****************************************
for(j;j<arrSize;j++)
{
temp=stations[j].m_clients.m_head;
while(temp!=NULL)
{
tempNext=temp->m_next;
free(temp);
temp=tempNext;
}
}
free(stations);
return 0;
}
this is the main of our program that shold get one simoulation file and unknow number of station files and initilizing the data structure from them.
but when we try to run the project we get a "Debug Assertion Failed" error.
please if you can help us to solve the priblem we need to submit the project by sunday.
thank you!
There is at least a bug here: stations[i].m_clients=*data;: i starts at 2, I believe.
Related
I have written the following file:
#include <stdio.h>
#include "config.h"
FILE* fptr;
void writeConfig(const char* new_api_key) {
if ((fptr = fopen("../config.txt", "w")) == NULL) {
printf("Error creating config file");
} else {
fprintf(fptr, "%s", new_api_key);
fclose(fptr);
}
}
char* readConfig(char* buffer, size_t bufSize) {
if ((fptr = fopen("../config.txt", "r")) == NULL) {
return "error opening config file";
} else {
if ((fgets(buffer, bufSize, fptr)) == NULL) {
fclose(fptr);
return "error reading config file";
}
else {
fclose(fptr);
return buffer;
}
}
}
And on 3 different tested machines this code works. But on one of my friends PC it does not.
The code gets executed as following:
char buffer[50];
long long bufSize = 50;
char* message = "example123";
writeConfig(message);
printf(readConfig(buffer, bufSize));
As seen in the "writeConfig" function, this creates a new txt file named "config.txt" and then tries to read it via the "readConfig" function. Weird is, that the "writeConfig" function does not throw an error, even tho the file is not created in his case. Only the "readConfig" function throws the error "error opening config file" which means that fopen() returned NULL.
We have also tried manually creating the config.txt file but the code still failed at the "readConfig" function with the same error.
I have the feeling that it could be about some read and write permissions? But i can not come up with a solution.
(Both PC run Windows)
Any help is appreciated :)
I have to read a text file, make some trivial transformation character by character (swapping the case of all letters), write results to the text files. I wrote this code, but it's not working. Please guide me in this regard. Thanks for in Advance
#include <stdio.h>
#include <stdlib.h>
int main() {
char c[1000];
char x[100];
char var;
int i;
FILE *fptr;
if ((fptr = fopen("text.txt", "r")) == NULL) {
printf("Error! opening file");
// Program exits if file pointer returns NULL...
exit(1);
}
// reads text until a newline is encountered...
fscanf(fptr, "%[^\n]", c);
printf("Data from the file:\n%s", c);
// Convert the file to upper case....
for( i=0;i<= strlen(c);i++){
if(c[i]>=65&&c[i]<=90)
c[i]=c[i]+32;
}
fptr = fopen("program.txt","w");
fprintf(fptr,"%[^\n]",c);
fclose(fptr);
return 0;
}
Edit: added #include <stdlib.h>, removed static describing main()
My proposition, based on example of copying a file given at my uni.
I used toupper() from ctype.h, if you don't want to use it you can just add 32 under condition similarly to your solution
Note: there could be char c instead of int c. (In the original version it actually was char; I changed it because if you look at the headers in the docs of all functions dealing with c, they all take/return int, not char; in your version it would matter more as you keep an array, in my program it changes pretty much nothing – int is just my preferred practice).
Note2: I actually never delved into the difference between "w"/"r" (write/read) and "wb"/"rb" (write/read binary). The code seems to work either way.
(I think there is no big difference when the files are text files anyway, for further assurance that both versions work, note that the code uses feof() to handle EOF)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
FILE *from, *to;
int c;//could be char
/* opening the source file */
if ((from = fopen("text.txt", "rb")) == NULL) {
printf("no such source file\n");
exit(1);
}
/* opening the target file */
if ((to = fopen("program.txt", "wb")) == NULL) {
printf("error while opening target file\n");
exit(1);
}
while (!feof(from)) {
c = fgetc(from);
if (ferror(from)) {
printf("error while reading from the source file\n");
exit(1);
}
if (!feof(from)) {//we avoid writing EOF
fputc(toupper(c), to);
if (ferror(to)) {
printf("error while writing to the target file\n");
exit(1);
}
}
}
if (fclose(from) == EOF) {
printf("error while closing...\n");
exit(1);
}
if (fclose(to) == EOF) {
printf("error while closing...\n");
exit(1);
}
return 0;
}
For a version taking arguments from command line (works on windows too) replace the beginning of main with
int main(int argc, char *argv[]) {
FILE *from, *to;
char c;
/* checking the number of arguments in the command line */
if (argc != 3) {
printf("usage: name_of_executable_of_this_main <f1> <f2>\n");//name_of_exe could be copy_to_upper, for example; change adequately
exit(1);
}
/* opening the source file */
if ((from = fopen(argv[1], "rb")) == NULL) {
printf("no such source file\n");
exit(1);
}
/* opening the target file */
if ((to = fopen(argv[2], "wb")) == NULL) {
printf("error while opening the target file\n");
exit(1);
}
I don't know how to code in that language(i think it's C++), but basically want you should be doing is a for loop to iterate through every character in the string. In Python it would look like:
x = open("text.txt", "r")
y = open("new text.txt","w")
z = ""
for char in x:
z += char.upper()
y.write(z)
I hope I was able to give an idea of how to solve your problem. I'm a newbie as well, but in Python.
I'm writing this using Xcode on my mac.
The file is definitely the correct location, and it opens it fine. That's not the problem.
I'm aiming to get a text file of words to be read in, and stored in the array list for future use, so the array acts as a dictionary.
However no cigar. Any help would be great!
#define MAX_WORDS 300000
#define MAX_BUFFER 50
int create_dictionary(void) {
FILE *fp = fopen("/Users/numberjak/Documents/Home/Projects/Python/anagram/words.txt", "r");
if (fp == NULL) {
printf("ERROR: File not found\n");
return 1;
}
char list[MAX_WORDS][MAX_BUFFER];
int i = 0;
while (fgets(list[i], MAX_BUFFER, fp) != NULL) {
printf(list[i]);
i++;
}
fclose(fp);
return 0;
}
I have a program that should take the file's name from command line using argc and argv. Printing argv[1] and argv[2] show me the exactly names I passed, but passing argv[1] and argv[2] as parameters to open the files in another function just show the error line I put if can't open the file.
My main:
int main(int argc, char* argv[])
{
if (argc != 4)
{
puts("Incorrect number of parameters.");
return 1;
}
else
{
Image *a, *b;
a = OpenFile(argv[1]);
b = OenFile(argv[2]);
} /* else */
return 0;
} /* main */
The function OpenFile will return a struct filled with information from the file. Here's the first part of the function:
Image *OpenFile(char* name)
{
FILE* f = fopen(name, "r");
Image* imagem;
int temp, i, cont, data[MAX];
char aux[2];
if(f == NULL)
{
puts("Error opening file.");
return NULL;
} /* if */
...
}
I'm passing the correct names but I receive the "Error opening file." line for each file I try to open.
Edit: It's giving me "No such file or directory", but I copied the files to the directory where the .exe is placed. It's not the first time I use the file's name from command line, but it's the first time I pass as parameters to another function.
From fopen man page:
RETURN VALUE
Upon successful completion fopen(), fdopen() and freopen() return a FILE pointer. Otherwise, NULL is returned and
errno is set to indicate the error.
So You could change:
if(f == NULL)
{
puts("Error opening file.");
return NULL;
} /* if */
With:
if(f == NULL)
{
perror("fopen");
return NULL;
} /* if */
And you ll get a pretty descriptive message on the reason fopen failed.
You should include errno.h in order to use perror
I am creating a project in C using Visual Studio and I need it to read a file and do an analysis on the text in that file. I have to pass the file to the command line, but I do not know where to place this file in order for it to be read. Here's the main method to help explain how this will be working:
int main(int argc, char *argv[]){
FILE * cmdFile;
char * nxtCmd = (char *) malloc(sizeof(char) * STR_LEN);
//Ensure memory allocation for nxtCmd succeeds
if (nxtCmd == NULL){
puts("Memory failed to allocate for nxtCmd.");
exit(1);
}
// check if argc is 2
if(argc == MAX_ARGS){}
else{
puts("Innapropriate number of arguments passed to the program.");
exit(1);
}
// check that argv[1] can be opened
if ((cmdFile = fopen(argv[1], "r")) == NULL){
puts("Can't open the command file.");
exit(1);
}
else{
puts("Command file has opened successfully.");
}
// if both above are true:
// start reading from the cmdfile
while ((nxtCmd = nextLine(cmdFile)) != NULL){
nxtCmd = nextLine(cmdFile);
printf("next line: %s", nxtCmd);
}
}
Any help or comments would be greatly appreciated.
Anywhere will do, just pass it to the program on the command line:
myprog.exe \path\to\file.txt