I'm a bit green in C and whole programming so I need help on task.
My goal is to read text file with random words(strings) and if there are any numbers in strings, change them to first letter of that word/string. Example: "He99llo Im N3w Her3" > "HeHHllo Im NNw HerH"
Questions: 1. How can I read separate strings from text file?
2. How can I get first letter (not number) from seperate strings?
By the way, I've wrote code, that takes only first char of text file and changes numbers into it, whenever it's number or not, but I dont know how to add code here...
EDIT: Here's the code written code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int Change(FILE *Reading, FILE *Writing){ /*Reads from file and changes numbers into first character of file*/
int FirstLetter, letter;
Reading = fopen("C:\\Users\\Rimas\\Desktop\\read.txt", "r");
Writing = fopen("C:\\Users\\Rimas\\Desktop\\write.txt", "w");
if (Writing == NULL) {
printf("I couldn't open write.txt for writing.\n");
exit(0);
}
FirstLetter = getc(Reading);
if (Reading){
fprintf(Writing, "%c", FirstLetter);
while ((letter = getc(Reading)) != EOF){
if (isdigit(letter)){
letter = FirstLetter;
}
fprintf(Writing, "%c", letter);
}
}
fclose(Reading);
fclose(Writing);
return 0;
}
int main() {
FILE *Reading;
FILE *Writing;
Change(Reading, Writing);
return 0;
}
Here's some logic to help you:
Write a function that accepts a string and converts it as you described. Do this first. And test it well because it is the fundamental part of the program.
Now you want to read a text file from main and simply display each word on screen in it's own line. This is to help you understand how to read words from a file. I would use fscanf for this. Make sure you can read the entire text file and it doesn't crash
Now right before printing the word that read in form step 2, call the function from step 1. This will alter the word and when you print you should get the correct results.
Related
I've been trying to figure out how I would, read a .txt file, and pick a line of said file from random then write the result to a different .txt file
for example:
.txt
bark
run
car
take line 2 and 3 add them together and write it to Result.txt on a new line.
How would I go about doing this???
I've tried looking around for resources for fopen(), fgets(), fgetc(), fprintf(), puts(). Haven't found anything so far on reading a line that isn't the first line, my best guess:
-read file
-print line of file in memory I.E. an array
-pick a number from random I.E. rand()
-use random number to pick a array location
-write array cell to new file
-repeat twice
-make newline repeat task 4-6
-when done
-close read file
-close write file
Might be over thinking it or just don't know what the operation to get a single line anywhere in a file is.
just having a hard time rapping my head around it.
I'm not going to solve the whole exercise, but I will give you a hint on how to copy a line from one file to another.
You can use fgets and increment a counter each time you find a line break, if the line number is the one you want to copy, you simply dump the buffer obtained with fgets to the target file with fputs.
#include <stdio.h>
#include <string.h>
int main(void)
{
// I omit the fopen check for brevity
FILE *in = fopen("demo.c", "r");
FILE *out = fopen("out.txt", "w");
int ln = 1, at = 4; // copy line 4
char str[128];
while (fgets(str, sizeof str, in))
{
if (ln == at)
{
fputs(str, out);
}
if (strchr(str, '\n') && (ln++ == at))
{
break;
}
}
fclose(in);
fclose(out);
return 0;
}
Output:
int main(void)
I am writing a program that inputs two text files
inputtxt1,
inputtxt2
and output
outputtxt file
In these two files information such as
input txt1
S00111111 5 6-Jul-19 09-Aug-19
S00800000 4 1-Jul-19 30-Aug-19
S00000000 1 1-Jul-19 30-Aug-19
input txt2
S00111111 3 6-Jul-19 09-Aug-19
S00222222 1 20-Jul-19 30-Aug-19
S00000000 1 1-Jul-19 30-Aug-19
I am writing a program to input these two txt files and output the differences in SQL queries and the values inside the bracket will change depends on the differences from these text files.
DELETE FROM TABLE WHERE TABLE=[] AND TABLE=[]
INSERT INTO TABLE (TABLE1,TABLE2,TABLE3,TABLE4) VALUES ([ ],[],'[2019-08-30] 00:00:00','[2019-07-01] 00:00:00');
DELETE FROM TABLE WHERE TABLE=[] AND TABLE=[4]
INSERT INTO TABLE (TABLE,TABLE) VALUES ([],[4]);
I wrote my draft in C so what I did id basically a while loop to read each of the line of the first file and each of the line of the second file and output the query.
Here are my two questions:
First it, unfortunately, output the file SQL 3 times, I think there is something wrong with my while loop.
Secondly, how would I make the program detect that specific character from specific line need to be printed in the query for example number 5 in the first line would detect and add to the value of one of the tables in the query.
/* This program will input two text files, output a text file with the differences*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
FILE *in1, *in2, *out;
int a, b;
void main (void)
{
int c;
char* singleline [33];
char* singleline2 [33];
in1 = fopen ("inputtest1.txt", "r"); /* reads from the first file */
in2 = fopen ("inputtest2.txt", "r"); /* reads from the second file */
out = fopen ("outputtest3", "w"); /* writes out put to this file */
// Menu //
printf ("TSC Support Program\n\n");
printf ("This program compare the two files and output the differences in SQL quries \n");
// if the file is empty or something went wrong!
if (in1 == NULL || in2 == NULL)
{
printf("********************Can Not Read File !**************************");
}
else
{
// Checking Every line in the first text file if it equals to the first line of the text file
while (!feof(in1)&&!feof(in2))
{
// a = getc(in1);
// b = getc(in2);
a = fgets(singleline, 33,in1);
b = fgets(singleline2, 33,in2);
if (a!=b)
{
printf("\n SQL will be printed\n");
fprintf (out,
"\n DELETE FROM BADGELINK WHERE BADGEKEY=[27] AND ACCLVLID=75"
"\nINSERT INTO BADGELINK (BADGEKEY,ACCLVLID,ACTIVATE,DEACTIVATE) VALUES ([27],75,'[2010-08-24] 00:00:00','[2010-12-17] 00:00:00'); \n"
"\n DELETE FROM BADGE WHERE BADGEKEY=[27] AND ISSUECODE=[75]"
"\nINSERT INTO BADGE (BADGEKEY,ISSUECODE) VALUES ([27],[1]);\n"
);
}
else
{
printf("Something went wrong");
}
}
}
fclose(in1);
fclose(in2);
fclose(out);
}
It prints the output 5 times
and then it says something went wrong. I am unsure what went wrong.
if (a != b) does not do what you think it is doing. Check strncmp() or memcmp() library functions.
But if you want to find out the first different character in two strings, the code below would do it for you.
Not tested properly, so take it as a quick prototype.
#include <stdio.h>
int strdiff(char *s1, char *s2){
char *p1 = s1;
while(*s1++ == *s2++)
;
if (s1 != s2)
return --s1-p1; /* we have s1++ in the while loop */
return -1;
}
int main(){
char *s1="S00111111 5 6-Jul-19 09-Aug-19";
char *s2="S00111111 3 6-Jul-19 09-Aug-19";
int i = strdiff(s1,s2);
printf("%d %c\n",i, s1[i]);
return 0;
}
Mind you, comparing two files line by line may turn out to be a bigger mission than it sounds if the two files you are comparing do not have exactly the same lines (with minor differences of course).
I am trying to code a game in C that deals with selection of races.
Each races has their own "stories" and when the user chooses to read one of their stories,
what I want to happen is,
While the program is running on Command Prompt, it will display the content I have typed in that specific text file about the story of the selected race.
This is what I have done so far.
void Race(char nameRace[20])
{
int race_choice,race_choice2,race_story;
FILE *race;
FILE *race1;
FILE *race2;
FILE *race3;
printf("The Races: 1.Human 2.Elf 3.Orc\n");
printf("Press 1 for Details of Each Races or 2 for selection: ");
scanf("%d",&race_choice);
if (race_choice==1)
{
printf("Which Race do you wish to know about?\n\t1.The Human\n\t2.The Elf\n\t3.The Orc\n\t: ");
scanf("%d",&race_story);
if (race_story==1)
{
race1=fopen("race1.txt","r");
fgetc(race1); // This does not display what I have typed on the race1.txt file on Command prompt.
// And I plan to write 2~3 paragraphs on the race1.txt file.
printf("\nGo Back to the Selection?(1 to Proceed)\n ");
scanf("%d",&race_choice2);
if (race_choice2==1)
{
printf("\n\n");
Race(nameRace);
}
else
{
wrongInput(race_choice2);// This is part of the entire code I have created. This works perfectly.
}
}
}
}
Please help me? :) Please!
The functionality you seem to be lacking is the ability to read a text file and output it. So it might be a good idea to code up a function which does just this, and then you whenever you need to display the contents of a file you can just pass a file name to our function and let it take care of the work, e.g.
static void display_file(const char *file_name)
{
FILE *f = fopen(file_name, "r"); // open the specified file
if (f != NULL)
{
INT c;
while ((c = fgetc(f)) != EOF) // read character from file until EOF
{
putchar(c); // output character
}
fclose(f);
}
}
and then within your code would just call this as e.g.
display_file("orcs.txt");
fgetc function reads, and returns single character from file, it doesn't prints it.
So you will need to do following:
while(!feof(race1)) { // Following code will be executed until end of file is reached
char c = fgetc(race1); // Get char from file
printf("%c",c); // Print it
}
It will print contents of race1 char-by-char.
I think you'll probably want to read the file line by line, so it's best to use fgets() instead of fgetc().
Example:
while(!feof(race1)) // checks to see if end of file has been reached for race1
{
char line[255]; // temporarily store line from text file here
fgets(line,255,race1); // get string from race1 and store it in line, max 255 chars
printf("%s",line); // print the line from the text file to the screen.
}
If you replace fgetc(race1) with the chunk of code above, it may work. I have not tried running it but it should work.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How do I get the input to cut off or wrap around at a certain point?
Alrighty, so after a day and a bit of being on stackoverflow, I learned it's useful being on this site :) I ended up getting my program to work. I can get an unlimited amount of text files in on the command line and display them as well! So it looks like this
I have:
CMD Console
c:\Users\Username\Desktop> wrapfile.exe hello.txt how.txt. are.txt you.txt random.txt
Hello How are you doing today? I hope you're doing quite well. This is just a test to see how much I can fit on the screen.
I need:
CMD Console
c:\Users\Username\Desktop> wrapfile.exe hello.txt how.txt. are.txt you.txt random.txt
Hello How are you doing today?
I hope you're doing quite well.
This is just a test to see how
much I can fit on the screen.
Now, I wana build on this program. How would I get this new found text to wrap around? Like, if you wanted to make it that, every 40 characters or so, the text jumps to the next line... how could we go about doing something like that?
Thanks again!
Here's the code I'm working with:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int l = 1;
while(l != argc)
{
FILE *fp; // declaring variable
fp = fopen(argv[l], "rb");
l++;
if (fp != NULL) // checks the return value from fopen
{
int i = 1;
do
{
i = fgetc(fp); // scans the file
printf("%c",i);
printf(" ");
}
while(i!=-1);
fclose(fp);
}
else
{
printf("Error.\n");
}
}
}
Do you want to word-wrap your file?
IF that's the case, here's a high level sequence of steps that I would do
First, I'd load the file into a character array
Second, I'd generate a new file. I would print 40 characters to the new file, and then the \n char.
Depending on your requirement, you may or may not want to replace the old file with the new file.
if you just need to print word-wrapped text to the console, instead of step 2, you can just print 40 chars to the command line and then a \n, and repeat for the next 40
So im working on learning how to do file I/O, but the book I'm using is terrible at teaching how to receive input from a file. Below is is their example of how to receive input from a file, but it doesn't work. I have copied it word for word, and it should loop through a list of names until it reaches the end of the file( or so they say in the book), but it doesn't. In fact if I leave the while loop in there, it doesn't print anything.
#include <stdio.h>
#include <conio.h>
#define MAX 250
int main()
{
char name[MAX];
FILE*pRead;
pRead=fopen("test.txt", "r");
if (pRead==NULL)
{
printf("file cannot be opened");
}else
printf("contents of test.txt");
while(fgets(name,sizeof(name),pRead)!=NULL){
{
printf("%s\n",name);
fscanf(pRead, "%s", name);
}
getch();
}
Even online, every beginners tutorial I see does some variation of this, but I can't seem to get it to work even a little bit.
I believe your array is too small and therefore when you are reading fscanf overwrites memory causing bizarre behavior
If you just want to read the file - presuming now there is one name per line followed by newline in the input file - just read the file using fgets() instead.
#define MAXLINE 256
char name[MAXLINE];
while (fgets(name,sizeof(name),pRead)!=NULL)
{
// do whatever
}