Click here to Skip to main content
14,634,453 members
Rate this:
Please Sign up or sign in to vote.
See more:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
  FILE * fp1 = fopen("file.csv", "r");
  FILE * fp2 = fopen("new.csv", "w");
  char string[200], toFind[200], updatedWord1[200];
  char * line =  NULL;
  char * result = NULL;
  char * word1 = NULL;
  char * word2 = NULL;

  printf("Enter word present in file: ");
  scanf("%s", toFind);

  while(fgets(string, 200, fp1)){
    // line = strtok(string, "\n");
    result = strstr(string, toFind);
    word1 = strtok(string, ",");
    word2 = strtok(NULL, ",");
    if(result)
    {
      printf("Enter another word update: ");
      getchar();
      fgets(updatedWord1, 200, stdin);
      fprintf(fp2, "%s,%s", updatedWord1, word2);
      
    }
  }
  return 0;
}


What I have tried:

This code has one problem. I don't know how to deal with it. Whenever I write the updated word, the result printed in the file break the updated word and the old word with a newline. Like

Given Output
update word
,word1

Desired output
updated word, word1

Which changes should be made in order to write the words in one line?
Posted
Updated 4 days ago
v2
Rate this:
Please Sign up or sign in to vote.

Solution 4

As others already suggested, the culprit is fgets function which doesn't remove the newline at the end of the user input, see fgets - C++ Reference[^]:

A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.
   
Comments
Richard MacCutchan 3 days ago
   
My real 5.
CPallini 3 days ago
   
:-)
Thank you very much, Richard.
Rate this:
Please Sign up or sign in to vote.

Solution 1

We don't know: we can't run your code under exactly the same conditions you do - if only becasue we have no idea what you are typing to make it work (or not work)!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Looking at your output line
fprintf(fp2, "%s,%s", updatedWord1, word2);
I would hazard a guess that word2is terminated by a newline character which you need to remove before output
   
Comments
Richard MacCutchan 4 days ago
   
I think you mean updatedWord1 which is obtained by fgets.
CPallini 4 days ago
   
My virtual 5.
CHill60 3 days ago
   
D'oh - of course.
Richard MacCutchan 3 days ago
   
"D'oh". The most used word in my lexicon. :(
CHill60 3 days ago
   
I am finding that the older I get, the more I have to use it. I do get quite annoyed with myself sometimes, especially the more obvious mess-ups (like this!). But I do find a large glass of red wine makes the problem seem to go away :-)
Richard MacCutchan 3 days ago
   
So true.
Rate this:
Please Sign up or sign in to vote.

Solution 3

The correct answer is: use the debugger, they have some memory view functions in which you exactly see what your bugs is.

You should learn to do it yourself.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100