Click here to Skip to main content
15,884,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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 14-Sep-20 7:50am
v2

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.
 
Share this answer
 
Comments
Richard MacCutchan 15-Sep-20 2:49am    
My real 5.
CPallini 15-Sep-20 3:12am    
:-)
Thank you very much, Richard.
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!
 
Share this answer
 
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
 
Share this answer
 
Comments
Richard MacCutchan 14-Sep-20 11:53am    
I think you mean updatedWord1 which is obtained by fgets.
CPallini 14-Sep-20 13:43pm    
My virtual 5.
CHill60 15-Sep-20 6:36am    
D'oh - of course.
Richard MacCutchan 15-Sep-20 7:25am    
"D'oh". The most used word in my lexicon. :(
CHill60 15-Sep-20 8:48am    
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 :-)
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900