| | |
File input output please help :(
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
Hello ,, i need to write to opened file in w+ mode..
now the problem is that first off all i need to check first string in each line if he the same to value i want to enter , dont enter and print error
:
in another words i need to convert this input txt to data txt:
the first number is ID of person.. before i putting it to my data.txt
i need to check if ID alredy exist : how i do that?
What i am now using is this function to take line into string
lets say *buffer=getline(Pointer to data file)
ID=strtok(buffer," ");
strcmp(ID_original,ID)
But i have problems with it , lets say if the data file is empty and there is nothing to scan i got errors
now the problem is that first off all i need to check first string in each line if he the same to value i want to enter , dont enter and print error
:
in another words i need to convert this input txt to data txt:
33333333 Johnny Depp center 6 44444444 Sandra Bullock guard 5 77777777 Tom Cruise captain 2 88888888 Cameron Diaz other 2 22222222 Gwyneth Paltrow alto 2
i need to check if ID alredy exist : how i do that?
What i am now using is this function to take line into string
C Syntax (Toggle Plain Text)
char *getline(FILE *input) { char temp[MAX_LEN]; fgets(temp,MAX_LEN,input); temp[strlen(temp)-1]='\0'; return strdup(temp); }
lets say *buffer=getline(Pointer to data file)
ID=strtok(buffer," ");
strcmp(ID_original,ID)
But i have problems with it , lets say if the data file is empty and there is nothing to scan i got errors
temp[strlen(temp)-1]='\0'; assumes that there's always a '\n' at the end of line, and that could be not the case. C Syntax (Toggle Plain Text)
/* wanted MAX_LEN of data, is there something to read? */ if (fgets(temp, MAX_LEN, input) != NULL) { size_t net_len = strlen(temp) -1; /* trim the newline/return if it's there */ if (temp[net_len] == '\n') { temp[net_len] = '\0'; } } else { /* handle the error... */ }
Last edited by Aia; Dec 27th, 2008 at 12:24 pm.
•
•
Join Date: Mar 2008
Posts: 1,405
Reputation:
Solved Threads: 114
return strdup(temp); Bad idea, you should never return a pointer to allocated dynamic memory, as you can quite easily forget to release that memory once you've done with it. The correct way to do this is to add a parameter containing a pointer to a buffer which you can write to.
Instead of this:
C Syntax (Toggle Plain Text)
char *getline(FILE *input) { char temp[MAX_LEN]; fgets(temp,MAX_LEN,input); temp[strlen(temp)-1]='\0'; return strdup(temp); }
C Syntax (Toggle Plain Text)
int getline(FILE *input, char *ptr, int destSize) { char temp[MAX_LEN]; /* wanted MAX_LEN of data, is there something to read? */ if (fgets(temp, MAX_LEN, input) != NULL) { size_t net_len = strlen(temp) -1; /* trim the newline/return if it's there */ if (temp[net_len] == '\n') { temp[net_len] = '\0'; } } else { /* handle the error... */ } strcpy_s(ptr, destSize, temp); }
Last edited by William Hemsworth; Dec 27th, 2008 at 4:10 pm.
> Bad idea, you should never return a pointer to allocated dynamic memory
What does malloc do then?
There's nothing inherently wrong with returning a duplicated copy of the string in allocated memory, so long as it's documented that the caller is responsible for calling free at some point.
The only dubious part of it was using the non-standard strdup() to do the copying.
What does malloc do then?
There's nothing inherently wrong with returning a duplicated copy of the string in allocated memory, so long as it's documented that the caller is responsible for calling free at some point.
The only dubious part of it was using the non-standard strdup() to do the copying.
•
•
Join Date: Mar 2008
Posts: 1,405
Reputation:
Solved Threads: 114
>There's nothing inherently wrong with returning a duplicated copy of the string in allocated memory
I will agree with that to some extent
but only if the caller knows exactly what he's doing, as by doing this, it makes your code more prone to errors. I'm quite sure it's bad practice to do this as i've caused myself problems in the past with memory leaks. One of my most common mistakes was something similar to this: It's better to be safe than sorry in my opinion
I will agree with that to some extent
C Syntax (Toggle Plain Text)
char *SubString(char *str, int beg, int fin) { char *sub = (char*)malloc( fin - beg + 1 ); // Rest of the code return sub; } int main() { // Oops someFunction( SubString("abcdef", 2, 4) ); }
Last edited by William Hemsworth; Dec 27th, 2008 at 6:50 pm.
>It's better to be safe than sorry in my opinion
Don't touch that keyboard then.
As a serious note:
Don't touch that keyboard then.

As a serious note:
char *sub = (char*)malloc( fin - beg + 1 ); casting malloc() is not necessary if the proper header file is included. Last edited by Aia; Dec 27th, 2008 at 8:27 pm.
![]() |
Similar Threads
- Server file input? (Java)
- Getting all data from an input and output file (C++)
- Saving to a file (C)
- File I/O (Java)
- C file input/output 2D arrays. (C)
- file input and output (C)
Other Threads in the C Forum
- Previous Thread: letter pyramid
- Next Thread: How to merge 2 strings ?
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation owf pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






