file handling: the code stops running - c++

#include <stdio.h>
main()
{
FILE* fp;
fp = fopen("a.text", "w+");
int yes = 1, count = 0;
char tree[30];
while (yes) {
printf("enter the name of the tree\n");
scanf("%s", tree);
fprintf(fp, "%s\n", tree);
printf("do you want to enter more trees?enter 0 if no and 1 if yes\n");
scanf("%d", &yes);
count++;
}
char treeArr[count][30];
int i = 0;
for (i = 0; i < count; i++) {
fscanf(fp, "%s", &treeArr[i]);
printf("%s", treeArr[i]);
}
fclose(fp);
}
this part of the code is not working.
char treeArr[count][30];
int i = 0;
for (i = 0; i < count; i++) {
fscanf(fp, "%s", &treeArr[i]);
printf("%s", treeArr[i]);
}
I need to write the names of the trees in the file and and then put it into a string array, so that i can calculate the percentage of each tree.
this is the question. kindly provide a suitable solution.
1.As you walk along TU campus, you see lots of trees of different species on your way.
Horticulture Centre has prepared a list of every tree standing on the way from CSE Department to the Main Gate.
You have to report the percentage of each tree species.
Input
Input to your program consists of a list of the species of every tree observed by the Horticulture Department; one tree per line in .txt file. No species name exceeds 30 characters.
Output
Print the name of each tree species available in TU (sorted in order of their percentage), followed by the percentage of the population it represents.

Problem is your file pointer fp is pointing to the end of file after the while loop (where you write into the file). So the subsequent read operations (fscanf) going to return EOF immediately.
You can reset the file pointer to the beginning before reading the file:
fseek(fp, 0L, SEEK_SET);
You would have noticed this if error check your library calls (fopen, scanf, fscanf, etc).

%s specifier expexts the char* but what you are passing is char (*)[30]. Even if they point to same memory they are different types.
Remove reference operator
fscanf(fp,"%s",treeArr[i]);
^^^^^^^^^^
Also you should rewind/reset file pointer of fp file descriptor which,after loop, point to the end of file what causes that fscanf returns EOF.
fseek(fp, 0L, SEEK_SET);
// Or
rewind(fp);

Related

fgets and sscanf to read file into an array

I am trying to implement a function that reads txt files that contain 2 double and int per line with a space as a delimiter, for example (data.txt):
3.1 2.5 1
3.4 4.5 2
.....
I implemented the function below, it takes as arguments the file name, pointers to 2 double and an int. Each pointer will be used to retrieve a column from the file. I used
fgets
to get the line and then
sscanf
to extract the three values from the line. The program crushes at run time (error: Windows has triggered a breakpont in program.exe )
... When I put a breaking point before returning from the main function the retrieved values are corrected but the program crushes when I hit continue.
To debug the function I added the following lines of code to the function ReadFile below:
if (buf[strlen(buf)-1] != '\0')
{
putchar(buf[strlen(buf)-1]); //surprisingly this line prints 1 instead of a space...
exit(1);
}
With this change the code exit without reaching the end of the function. I don't understand this because the buf variable is long enough to hold all the characters of one line from the sample file.
I would appreciate your comments to fix this problem.
Thank you.
bool ReadFile(const char* fileName, double* x, double* y, int* t, int size)
{
FILE* fp;
char buf[5000];
fp = fopen( fileName, "r" );
for( int i = 0; i < size && fgets(buf, 5000, fp) != NULL; i++)
{
buf[strlen(buf)-1] = '\0';
if (buf[strlen(buf)-2] != '\0')
{
putchar(buf[strlen(buf)-1]);//surprisingly this line prints 1 instead of a space...
exit(1);
}
sscanf(buf, "%lf %lf %d", &x[i], &y[i], &t[i]);
}
fclose(fp);
return true;
}
The problem was in the caller, the size was set to 1 instead of the number of lines in the data file.

Reading and printing characters from a user defined text file

I am trying to work out how I can print character by character the contents of a user-defined text file. I believe I have got the retrieval of the file correct but I am unsure how I can print each character.
#include <stdio.h>
#include <ctype.h>
#define ELEMENT 300
#define LENGTH 20
void main(char str[ELEMENT][LENGTH])
{
FILE *infile;
char textfile[1000];
char read_char;
int endoff;
int poswithin = 0;
int wordnum= 0;
printf("What is the name of your text file?: ");
scanf("%s", &textfile);
infile=fopen(textfile,"r");
if (infile == NULL) {
printf("Unable to open the file.");
}
else
{
endoff=fscanf(infile,"%c",&read_char);
while(endoff!=EOF);
{
This is where I believe I'm stuck. The first character is read into the variable read_char but then it doesn't seem to print anything?
if(read_char>=65&&read_char<=90 || read_char<=65)
{
str[wordnum][poswithin]=read_char;
printf("%c", read_char);
poswithin++;
}
else
{
str[wordnum][poswithin]=(char)"\n";
poswithin=0; wordnum++;
}
endoff=fscanf(infile, "%s", &read_char);
}
}
fclose(infile);
}
Typo in the format specifier to your second call to fscanf
endoff=fscanf(infile, "%s", &read_char);
should be
endoff=fscanf(infile, "%c", &read_char);
Also,
str[wordnum][poswithin]=(char)"\n";
shouldn't be casting a string literal to char and probably should be adding a NULL terminator rather than a newline:
str[wordnum][poswithin]='\0';
Finally, you shouldn't try to declare str as an argument to main.
char str[ELEMENT][LENGTH];
int main() // or int main(int argc, char* argv[])
Using fscanf with %c format specifier is overkill for reading a single character from a file.
Try fgetc to read one character. The function avoids the overhead of parsing a format specifier string and variable number of arguments.
A more efficient method is to allocate a buffer or array and read "chunks" of chars from a file, using fread. You can then scan the buffer or array. This has less function call overhead than many calls to read single bytes. Efficient buffer sizes are multiples of 512 to conform with disk drive sector sizes.

Searching for a word in a text using C, and display the info after that word

Say I have a text file like this:
User: John
Device: 12345
Date: 12/12/12
EDIT:
I have my code to successfully search for a word, and display the info after that word. However when I try to edit the code to search for 2 or 3 words and display the info after them instead of just 1 word, I cannot get it to work. I have tried adding codes into the same while loop, and creating a new while loop for the other word, but both doesn't work. There must be something I am doing wrong/not doing.
Please advice, thanks!
Here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char file[100];
char c[100];
printf ("Enter file name and directory:");
scanf ("%s",file);
FILE * fs = fopen (file, "r") ;
if ( fs == NULL )
{
puts ( "Cannot open source file" ) ;
exit( 1 ) ;
}
FILE * ft = fopen ( "book5.txt", "w" ) ;
if ( ft == NULL )
{
puts ( "Cannot open target file" ) ;
exit( 1 ) ;
}
while(!feof(fs)) {
char *Data;
char *Device;
char const * rc = fgets(c, 99, fs);
if(rc==NULL) { break; }
if((Data = strstr(rc, "Date:"))!= NULL)
printf(Data+5);
if((Data = strstr(rc, "Device:"))!=NULL)
printf(Device+6);
}
fclose ( fs ) ;
fclose ( ft ) ;
return 0;
}
Ok, hope I can clear it this time. Sorry if I get confusing sometimes but my english is not the best.
I'll explain the implementation inside comments:
#define BUFFSIZE 1024
int main()....
char buff[BUFFSIZE];
char delims[] = " "; /*Where your strtok will split the string*/
char *result = NULL;
char *device; /*To save your device - in your example: 12345*/
char *date; /*To save the date*/
int stop = 0;
fp = fopen("yourFile", "r");
while( fgets(buff, BUFFSIZE,fp) != NULL ) /*This returns null when the file is over*/
{
result = strtok( buff, delims ); /*You just need to do reference to buff here, after this, strtok uses delims to know where to do the next token*/
while(result != NULL){ /*Strtok returns null when finishes reading the given string*/
if(strcmp(result,"Device")==0){ /*strcmp returns 0 if the strings are equal*/
result = strtok(NULL, delims); /*this one gets the 12345*/
device = (char*)malloc((strlen(result)+1)*sizeof(char)); /*Alocate the right amount of memory for the variable device*/
strcpy(device, result); /*Now, device is "12345"*/
}
/*Here you do the same but for the string 'Date'*/
if(strcmp(result,"Date")==0){ /*strcmp returns 0 if the strings are equal*/
result = strtok(NULL, delims); /*this one gets the 12345*/
date = (char*)malloc((strlen(result)+1)*sizeof(char)); /*Alocate the right amount of memory for the variable device*/
strcpy(date, result); /*Now, device is "12/12/12"*/
}
/*And you can repeat the if statement for every string you're looking for*/
result = strtok(NULL,delims); /*Get the next token*/
}
}
/*No strtok necessary here */
...
Hope this helps.
fgetc returns an integer value, which is character, promoted to int.
I suppose you meant fgets which reads a whole line, but you need to reserve memory for it, for example:
#define BUF 100
...
char c[BUF];
fgets(c, BUF, fs);
Some helpful links.
There are a couple of problems in your code: basically it never compiled.
Here is a version with small cleanups - which at least compiles:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char file[100];
char c[100];
printf ("Enter file name and directory:");
scanf ("%s",file);
FILE * fs = fopen (file, "r") ;
if ( fs == NULL ) {
puts( "Cannot open source file" ) ;
exit(1 ) ;
}
while(!feof(fs)) {
char *Data;
char const * rc = fgets(c, 99, fs);
if(rc==NULL) { break; }
if((Data = strstr(rc, "Device"))!= NULL)
printf("%s", Data);
}
fclose ( fs ) ;
return 0;
}
Problems I found:
Missing include for exit()
Missing parameter for exit()
Missing while loop to run through the whole input file.
The output file was never used.
Missing return value of 'main'
Fancy Data[5]
Changed fgetc() to fgets()
I only did minimal edits - it's not perfect at all....
IMHO I would go for C++: many things are much simpler there.
If printf() isn't a hard/fast rule, and the input requirements are really this simple, I'd prefer a state-machine and a constant-memory input:
int c, x = 0; // c is character, x is state
while(EOF!=(c=getchar())){ // scanner entry point
if(c == '\n') x=0; // newline resets scanner
else if(x == -1) continue; // -1 is invalid state
else if (x < 7 && c=="Device:"[x])x++; // advance state
else if (x == 7 && isspace(c)) continue; // skip leading/trailing whitespace
else if (x == 7) putchar(c); // successful terminator (exits at \n)
else x = -1; // otherwise move to invalid state
}
I would do that with two loops: one to get a line from the file and other to make tokens from the line read.
something like:
#define BUFFSIZE 1024
int main()....
char buff[BUFFSIZE];
char delims[] = " ";
char *result = NULL;
int stop = 0;
fp = fopen("yourFile", "r");
while( fgets(buff, BUFFSIZE,fp) != NULL ) /*This returns null when the file is over*/
{
result = strtok( buff, delims ); /*You just need to do reference to buff here, after this, strtok uses delims to know where to do the next token*/
while(result != NULL){ /*Strtok returns null when finishes reading the given string*/
if(strcmp(result,"Device")==0){ /*strcmp returns 0 if the strings are equal*/
stop = 1; /*Update the flag*/
break; /*Is now possible to break the loop*/
}
result = strtok(NULL,delims); /*Get the next token*/
}
if(stop == 1) break; /*This uses the inside flag to stop the outer loop*/
}
result = strtok(NULL, delims); /*Result, now, has the string you want: 12345 */
...
this code is not very accurate and I didn't tested it, but thats how I would try to do it.
Hope this helps.
My suggestion is to use fread to read all the file.You could read it character by character, but IMHO (a personal taste here) it's simpler to get a string containing all the characters and then manipulating it.
This is the function prototype:
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
It returns the number of elements read.
For example:
char buffer[100];
size_t n= fread(buffer, 1,100, fs);
Then you can manipulate the string and divide it in tokens.
EDIT
There is a nice reference with also an example of how dividing a string into tokens here:
http://www.cplusplus.com/reference/cstring/strtok/
c and Data are char-pointers, pointers to (the start of a list of) character value(s).
fgetc's prototype is int fgetc ( FILE * stream ); meaning that it returns (one) integer value (an integer is convertible to a single char value).
If fgetc's prototype would've been int * fgetc ( FILE * stream ); the warning wouldn't have appeared.
#Dave Wang
My answer was too big to be a comment. So here it goes:
You're welcome. Glad to help.
If you make a new loop, the fgets won't work because you are already 'down' in the text file. Imagine something like a pointer to the file, every time you 'fget it' from a file pointer, you advance that pointer. You have functions to reload the file or push that pointer up, but it is not efficient, you've already passed by the information you want, there must be a way to know when.
If you're using my implementation, that is done by using another string compare inside the loop:
if(strcmp(result,"date") == 0)
If you enter this if, you know that the next value in result token with strtok is the actual date.
Since you have now two conditions to be tested, you can't break the outer loop before having both of them. This can be accomplished by two ways:
1-Instead of a flag, use a counter that is incremented everytime you want an information. If that counter has the same number of information you want, you can break the outer loop.
2-Don't break the outer loop at all! :)
But in both, since there are 2 conditions, make sure you treat them inside the ifs so you know that you dealing with the right information.
Hope this helps. Anything, just ask.

Can't write an integer into a binary file C++

This is basically the part of the code that i used to store the entire file, and works well ... but when i tryed to store a integer bigger than 120 or something like that the program writes seems like a bunch of trash and not the integer that i want. Any tips ? I am an college student and dont have a clue whats happening.
int* temp
temp = (int*) malloc (sizeof(int));
*temp = atoi( it->valor[i].c_str() );
//Writes the integer in 4 bytes
fwrite(temp, sizeof (int), 1, arq);
if( ferror(arq) ){
printf("\n\n Error \n\n");
exit(1);
}
free(temp);
I've already checked the atoi part and it really returns the number that I want to write.
I changed and added some code and it works fine:
#include <iostream>
using namespace std;
int main()
{
int* temp;
FILE *file;
file = fopen("file.bin" , "rb+"); // Opening the file using rb+ for writing
// and reading binary data
temp = (int*) malloc (sizeof(int));
*temp = atoi( "1013" ); // replace "1013" with your string
//Writes the integer in 4 bytes
fwrite(temp, sizeof (int), 1, file);
if( ferror(file) ){
printf("\n\n Error \n\n");
exit(1);
}
free(temp);
}
Make sure you are opening the file with the correct parameters, and that the string you give to atoi(str) is correct.
I checked the binary file using hex editor, after inputting the number 1013.
int i = atoi("123");
std::ofstream file("filename", std::ios::bin);
file.write(reinterpret_cast<char*>(&i), sizeof(i));
Do not use pointers here.
Never use malloc / free in C++.
Use C++ file streams, not C streams.

What is the proper method of reading and parsing data files in C++?

What is an efficient, proper way of reading in a data file with mixed characters? For example, I have a data file that contains a mixture of data loaded from other files, 32-bit integers, characters and strings. Currently, I am using an fstream object, but it gets stopped once it hits an int32 or the end of a string. if i add random data onto the end of the string in the data file, it seems to follow through with the rest of the file. This leads me to believe that the null-termination added onto strings is messing it up. Here's an example of loading in the file:
void main()
{
fstream fin("C://mark.dat", ios::in|ios::binary|ios::ate);
char *mymemory = 0;
int size;
size = 0;
if (fin.is_open())
{
size = static_cast<int>(fin.tellg());
mymemory = new char[static_cast<int>(size+1)];
memset(mymemory, 0, static_cast<int>(size + 1));
fin.seekg(0, ios::beg);
fin.read(mymemory, size);
fin.close();
printf(mymemory);
std::string hithere;
hithere = cin.get();
}
}
Why might this code stop after reading in an integer or a string? How might one get around this? Is this the wrong approach when dealing with these types of files? Should I be using fstream at all?
Have you ever considered that the file reading is working perfectly and it is printf(mymemory) that is stopping at the first null?
Have a look with the debugger and see if I am right.
Also, if you want to print someone else's buffer, use puts(mymemory) or printf("%s", mymemory). Don't accept someone else's input for the format string, it could crash your program.
Try
for (int i = 0; i < size ; ++i)
{
// 0 - pad with 0s
// 2 - to two zeros max
// X - a Hex value with capital A-F (0A, 1B, etc)
printf("%02X ", (int)mymemory[i]);
if (i % 32 == 0)
printf("\n"); //New line every 32 bytes
}
as a way to dump your data file back out as hex.