Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I use fgetc to read characters from a file and it reads something strange.
I have this in the txt file:
But when i look at the file info it shows this:
and fgetc actually reads the { as first character of the file instead of S.
I tried to change the file extension to rtf,txt, no extension but same result.
Any ideas how to fix this?
Looks like you wrote the file in some sort of editor that stores its metadata at the beginning of the file. Try recreating the file with a more basic editor that does just text editing. Or from the command line echo "my file contents" > myfilename.txt.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a script that writes about 20 numbers line by line to a file during processing.
When the script starts again, it reads from this file with this code
declare -a sedum
i=0
while read -r line
do
sedum[$i]=$line
i=$(( $i + 1 ))
done < $f_sday
f_sday contains the filename. When I call the script from comand line it always works fine and reads the complete content of the file.
But when the script is called in a cronjob it reads only two or three values
I know that from cron it might not be the same environment but I can't see any environmental dependency here.
I tried mapfile at first, but that read only two of the twenty values.
Any idea what I am missing here?
Stupid me.
I did not control the working path (cron starts in $HOME), so the script was working on the wrong file.
Thanks for the set -x hint. That led me on the right path!
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am a beginner at C using Eclipse, and am trying to read 2 file and write it so that there are 4 words on each line. This code belongs to a function and I am calling it on 2 different files. I have opened and closed the input file fin and the output file fout. Both files exist.
char word[15];
int i = 0;
while (fscanf(fin, "%s", word) != EOF) {
fprintf(fout, "%s ", word);
i++;
if (i > 3) {
fprintf(fout, "\n");
i = 0;
}
}
This code works if I write to console instead of a file. However, when I run it as shown here so that it writes to the output file, nothing is written in the file. Printing word to file has no issues without the incrementation of i.
I've printed out the values of i and word to console directly after the incrementation, and it appears that i is being reset correctly. File 1 appears to have been read completely and written to console, but nothing was written to the output file. For file 2, the code prints everything to console correctly until it hits a comma, and nothing is written to output. When the comma is removed, a few lines of file 1 are written to file and file 2 prints but nearly all the words are missing letters.
What is wrong here? I'm not sure if there's a local issue because this code worked a few hours ago. Thank you.
Issue solved, there were words in the file longer than 14 chars and didn't fit correctly into my string variable.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I can't understand the syntax of writing in files in C.
I was requested in class to open a file named Numbers.txt and then print from 1 to 10 and each new line. I dont understand the syntax of it.
We use fopen,fwrite,fclose.
need help with understanding how to work with it.
You use a FILE to assign a file to something you can use. fopen opens a file depending on if the file exists or not.
FILE *pFile = fopen("Filename.foo","r");
This opens the file that is located in your project with the same filename. The 2nd parameter is the mode of the file — how you want to use it. Here is a table of modes. You must always check the return value to ensure it is not NULL, indicating that the program was unable to open the file as requested.
You always want to close the file when you're done editing it by using fclose.
fclose(pFile);
fwrite is one way of writing to a file; fprintf and family are another. Basically think about it like you opened a text editor and typed something in the file. You need to create a buffer (a place to store the numbers), then write the numbers into the file. Here is the reference for fwrite. I can't do all of your homework.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I made a program which takes numbers from the command line and adds them, the program is supposed to print an error if something else than a number is written, but when it reads > or < it creates text files with the results or rewrites on an already existing text file, if it doesn't exist it just stops without even running the code, is there a way to stop this from happening and read it just like another array?
Here is an example of the error
$ ./a +24 < 5
bash: 5: no such file or directory
That's not your program - that's the shell file I/O redirection. If you want your program to see the < or >, escape them appropriately:
./a +24 \<5
As ths others have said, it is not your program acting up, but the command line shell uses < and > as input/output redirection operators. You could escape them with backslashes.
But rather than forcing the users to escape each < and > (and possibly some other special characters like $ and the parentheses), you can quote the whole command line:
./a '1 < 24'
The whole command now is in argv[1]. You still have to parse it. As a bonus, there is no need to insert annoying spaces between the tokens anymore:
./a '1<24'
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a shell script that uses an array. The script cycle through the entries of the array but for some reason the first and last entry has a problem.
The array:
Queue_Names=( CLQueue DLQ ExpiryQueue )
The for Loop:
for i in “${Queue_Names[#]}”
do
#do stuff
done
I can see in the console and shows that for the first entry it shows: �CLQueue.
The last entry shows: ExpiryQueue�
I'm guessing these are markers to know the start and end of the array. Unfortunately it is interfering with the functionality of the script. I use these Queue names to search for something and it fails to find it because of the added character. How do I get rid of them or is there a code change I do to avoid the problem?
“${Queue_Names[#]}” is not "${Queue_Names[#]}", because “” is not "".
"Smart quotes" aren't recognized as quotes at all in bash; thus, the effect is the same as if the expansion had been unquoted -- string-splitting and glob-expansion on array contents -- with the literal "quotes" grafted around the start and end characters.
You need to use real quotes -- "" -- not opening/closing "smart quotes" created by some word processing software or corporate email tools.