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:

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

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

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

> lets say if the data file is empty and there is nothing to scan i got errors
Well you need to pay attention to the return result of fgets() before you do anything else.

temp[strlen(temp)-1]='\0'; assumes that there's always a '\n' at the end of line, and that could be not the case.

/* 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... */
}

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:

char *getline(FILE *input)
{
  char temp[MAX_LEN];
  fgets(temp,MAX_LEN,input);
  temp[strlen(temp)-1]='\0';
  return strdup(temp);
}

Try:

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);
}

Hope this helps :icon_cheesygrin:

> 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.

>There's nothing inherently wrong with returning a duplicated copy of the string in allocated memory

I will agree with that to some extent :icon_wink: 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:

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) );
}

It's better to be safe than sorry in my opinion :icon_cheesygrin:

It's better to be safe than sorry in my opinion

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.

wow guys tnx ... nice forum also

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.